diff --git a/README.md b/README.md
index 5d8d5bbc..6ea50530 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,7 @@
* **[Face Recognition](#about-face-recognition)**
* **[Face Landmark Detection](#about-face-landmark-detection)**
* **[Usage](#usage)**
+ * **[Loading the Models](#usage-load-models)**
* **[Face Detection](#usage-face-detection)**
* **[Face Recognition](#usage-face-recognition)**
* **[Face Landmark Detection](#usage-face-landmark-detection)**
@@ -95,19 +96,46 @@ Or install the package:
npm i face-api.js
```
-
+
-### Face Detection
+### Loading the Models
+
+To load a model, you have provide the corresponding manifest.json file as well as the model weight files (shards) as assets. Simply copy them to your public or assets folder. The manifest.json and shard files of a model have to be located in the same directory / accessible under the same route.
+
+Assuming the models reside in **public/model**:
+
+``` javascript
+const net = new faceapi.FaceDetectionNet()
+// accordingly for the other models:
+// const net = new faceapi.FaceLandmarkNet()
+// const net = new faceapi.FaceRecognitionNet()
-Download the weights file from your server and initialize the net (note, that your server has to host the *face_detection_model.weights* file).
+await net.load('/models/face_detection_model-weights_manifest.json')
+// await net.load('/models/face_landmark_68_model-weights_manifest.json')
+// await net.load('/models/face_recognition_model-weights_manifest.json')
+
+// or simply
+await net.load('/models')
+```
+
+Alternatively you can load the weights as a Float32Array (in case you want to use the uncompressed models):
``` javascript
-// initialize the face detector
-const res = await axios.get('face_detection_model.weights', { responseType: 'arraybuffer' })
+// using fetch
+const res = await fetch('/models/face_detection_model.weights')
+const weights = new Float32Array(await res.arrayBuffer())
+net.load(weights)
+
+// using axios
+const res = await axios.get('/models/face_detection_model.weights', { responseType: 'arraybuffer' })
const weights = new Float32Array(res.data)
-const detectionNet = faceapi.faceDetectionNet(weights)
+net.load(weights)
```
+
+
+### Face Detection
+
Detect faces and get the bounding boxes and scores:
``` javascript
@@ -141,15 +169,6 @@ const { boxes, scores } = detectionNet.forward('myImg')
### Face Recognition
-Download the weights file from your server and initialize the net (note, that your server has to host the *face_recognition_model.weights* file).
-
-``` javascript
-// initialize the face recognizer
-const res = await axios.get('face_recognition_model.weights', { responseType: 'arraybuffer' })
-const weights = new Float32Array(res.data)
-const recognitionNet = faceapi.faceRecognitionNet(weights)
-```
-
Compute and compare the descriptors of two face images:
``` javascript
@@ -180,15 +199,6 @@ const t = recognitionNet.forward('myImg')
### Face Landmark Detection
-Download the weights file from your server and initialize the net (note, that your server has to host the *face_landmark_68_model.weights* file).
-
-``` javascript
-// initialize the face recognizer
-const res = await axios.get('face_landmark_68_model.weights', { responseType: 'arraybuffer' })
-const weights = new Float32Array(res.data)
-const faceLandmarkNet = faceapi.faceLandmarkNet(weights)
-```
-
Detect face landmarks:
``` javascript
diff --git a/dist/commons/isTensor.d.ts b/dist/commons/isTensor.d.ts
new file mode 100644
index 00000000..ab251612
--- /dev/null
+++ b/dist/commons/isTensor.d.ts
@@ -0,0 +1,6 @@
+import * as tf from '@tensorflow/tfjs-core';
+export declare function isTensor(tensor: tf.Tensor, dim: number): boolean;
+export declare function isTensor1D(tensor: tf.Tensor): boolean;
+export declare function isTensor2D(tensor: tf.Tensor): boolean;
+export declare function isTensor3D(tensor: tf.Tensor): boolean;
+export declare function isTensor4D(tensor: tf.Tensor): boolean;
diff --git a/dist/commons/isTensor.js b/dist/commons/isTensor.js
new file mode 100644
index 00000000..d5b70658
--- /dev/null
+++ b/dist/commons/isTensor.js
@@ -0,0 +1,17 @@
+import * as tf from '@tensorflow/tfjs-core';
+export function isTensor(tensor, dim) {
+ return tensor instanceof tf.Tensor && tensor.shape.length === dim;
+}
+export function isTensor1D(tensor) {
+ return isTensor(tensor, 1);
+}
+export function isTensor2D(tensor) {
+ return isTensor(tensor, 2);
+}
+export function isTensor3D(tensor) {
+ return isTensor(tensor, 3);
+}
+export function isTensor4D(tensor) {
+ return isTensor(tensor, 4);
+}
+//# sourceMappingURL=isTensor.js.map
\ No newline at end of file
diff --git a/dist/commons/isTensor.js.map b/dist/commons/isTensor.js.map
new file mode 100644
index 00000000..3d356cb2
--- /dev/null
+++ b/dist/commons/isTensor.js.map
@@ -0,0 +1 @@
+{"version":3,"file":"isTensor.js","sourceRoot":"","sources":["../../src/commons/isTensor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAE5C,MAAM,mBAAmB,MAAiB,EAAE,GAAW;IACrD,OAAO,MAAM,YAAY,EAAE,CAAC,MAAM,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,KAAK,GAAG,CAAA;AACnE,CAAC;AAED,MAAM,qBAAqB,MAAiB;IAC1C,OAAO,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;AAC5B,CAAC;AAED,MAAM,qBAAqB,MAAiB;IAC1C,OAAO,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;AAC5B,CAAC;AAED,MAAM,qBAAqB,MAAiB;IAC1C,OAAO,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;AAC5B,CAAC;AAED,MAAM,qBAAqB,MAAiB;IAC1C,OAAO,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;AAC5B,CAAC"}
\ No newline at end of file
diff --git a/dist/commons/loadWeightMap.d.ts b/dist/commons/loadWeightMap.d.ts
new file mode 100644
index 00000000..7bd9921f
--- /dev/null
+++ b/dist/commons/loadWeightMap.d.ts
@@ -0,0 +1,5 @@
+export declare function getModelUris(uri: string | undefined, defaultModelName: string): {
+ manifestUri: string;
+ modelBaseUri: string;
+};
+export declare function loadWeightMap(uri: string | undefined, defaultModelName: string): Promise;
diff --git a/dist/commons/loadWeightMap.js b/dist/commons/loadWeightMap.js
new file mode 100644
index 00000000..69a71dd3
--- /dev/null
+++ b/dist/commons/loadWeightMap.js
@@ -0,0 +1,32 @@
+import * as tslib_1 from "tslib";
+import * as tf from '@tensorflow/tfjs-core';
+export function getModelUris(uri, defaultModelName) {
+ var parts = (uri || '').split('/');
+ var modelBaseUri = ((uri || '').endsWith('.json')
+ ? parts.slice(0, parts.length - 1)
+ : parts).filter(function (s) { return s; }).join('/');
+ var defaultManifestFilename = defaultModelName + "-weights_manifest.json";
+ var manifestUri = !uri || !modelBaseUri
+ ? defaultManifestFilename
+ : (uri.endsWith('.json')
+ ? uri
+ : modelBaseUri + "/" + defaultManifestFilename);
+ return { manifestUri: manifestUri, modelBaseUri: modelBaseUri };
+}
+export function loadWeightMap(uri, defaultModelName) {
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
+ var _a, manifestUri, modelBaseUri, manifest;
+ return tslib_1.__generator(this, function (_b) {
+ switch (_b.label) {
+ case 0:
+ _a = getModelUris(uri, defaultModelName), manifestUri = _a.manifestUri, modelBaseUri = _a.modelBaseUri;
+ return [4 /*yield*/, fetch(manifestUri)];
+ case 1: return [4 /*yield*/, (_b.sent()).json()];
+ case 2:
+ manifest = _b.sent();
+ return [2 /*return*/, tf.io.loadWeights(manifest, modelBaseUri)];
+ }
+ });
+ });
+}
+//# sourceMappingURL=loadWeightMap.js.map
\ No newline at end of file
diff --git a/dist/commons/loadWeightMap.js.map b/dist/commons/loadWeightMap.js.map
new file mode 100644
index 00000000..29de3a2a
--- /dev/null
+++ b/dist/commons/loadWeightMap.js.map
@@ -0,0 +1 @@
+{"version":3,"file":"loadWeightMap.js","sourceRoot":"","sources":["../../src/commons/loadWeightMap.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAE5C,MAAM,uBAAuB,GAAuB,EAAE,gBAAwB;IAC5E,IAAM,KAAK,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEpC,IAAM,YAAY,GAAG,CACnB,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC;QAC3B,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;QAClC,CAAC,CAAC,KAAK,CACV,CAAC,MAAM,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,EAAD,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IAE1B,IAAM,uBAAuB,GAAM,gBAAgB,2BAAwB,CAAA;IAC3E,IAAM,WAAW,GAAG,CAAC,GAAG,IAAI,CAAC,YAAY;QACvC,CAAC,CAAC,uBAAuB;QACzB,CAAC,CAAC,CACA,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC;YACnB,CAAC,CAAC,GAAG;YACL,CAAC,CAAI,YAAY,SAAI,uBAAyB,CACjD,CAAA;IAEH,OAAO,EAAE,WAAW,aAAA,EAAE,YAAY,cAAA,EAAE,CAAA;AACtC,CAAC;AAED,MAAM,wBACJ,GAAuB,EACvB,gBAAwB;;;;;;oBAGlB,KAAgC,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,EAAjE,WAAW,iBAAA,EAAE,YAAY,kBAAA,CAAwC;oBAEjD,qBAAM,KAAK,CAAC,WAAW,CAAC,EAAA;wBAA/B,qBAAM,CAAC,SAAwB,CAAC,CAAC,IAAI,EAAE,EAAA;;oBAAlD,QAAQ,GAAG,SAAuC;oBAExD,sBAAO,EAAE,CAAC,EAAE,CAAC,WAAW,CAAC,QAAQ,EAAE,YAAY,CAAC,EAAA;;;;CACjD"}
\ No newline at end of file
diff --git a/dist/face-api.js b/dist/face-api.js
index d4ff1285..e40d9852 100644
--- a/dist/face-api.js
+++ b/dist/face-api.js
@@ -1,16208 +1,2006 @@
(function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('crypto')) :
- typeof define === 'function' && define.amd ? define(['exports', 'crypto'], factory) :
- (factory((global.faceapi = global.faceapi || {}),global.crypto));
-}(this, (function (exports,crypto) { 'use strict';
-
- crypto = crypto && crypto.hasOwnProperty('default') ? crypto['default'] : crypto;
-
- function isMobile() {
- var a = navigator.userAgent || navigator.vendor || window.opera;
- return /(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(a) ||
- /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(a.substr(0, 4));
- }
-
- function doc(info) {
- return function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- };
- }
-
- function assertArgumentIsTensor(x, argName, functionName) {
- assert(x instanceof Tensor, "Argument '" + argName + "' passed to '" + functionName + "' must be a Tensor, " +
- ("but got " + typeof x + "."));
- }
- function assertArgumentsAreTensors(args, functionName) {
- var _loop_1 = function (argName) {
- var arg = args[argName];
- if (Array.isArray(arg)) {
- arg.forEach(function (t, i) {
- assertArgumentIsTensor(t, argName + "[" + i + "]", functionName);
- });
- }
- else {
- assertArgumentIsTensor(arg, argName, functionName);
- }
- };
- for (var argName in args) {
- _loop_1(argName);
- }
- }
- function shuffle(array) {
- var counter = array.length;
- var temp = 0;
- var index = 0;
- while (counter > 0) {
- index = (Math.random() * counter) | 0;
- counter--;
- temp = array[counter];
- array[counter] = array[index];
- array[index] = temp;
- }
- }
- function clamp(min, x, max) {
- return Math.max(min, Math.min(x, max));
- }
- function randUniform(a, b) {
- return Math.random() * (b - a) + a;
- }
- function distSquared(a, b) {
- var result = 0;
- for (var i = 0; i < a.length; i++) {
- var diff = Number(a[i]) - Number(b[i]);
- result += diff * diff;
- }
- return result;
- }
- function assert(expr, msg) {
- if (!expr) {
- throw new Error(msg);
- }
- }
- function assertShapesMatch(shapeA, shapeB, errorMessagePrefix) {
- if (errorMessagePrefix === void 0) { errorMessagePrefix = ''; }
- assert(arraysEqual(shapeA, shapeB), errorMessagePrefix + (" Shapes " + shapeA + " and " + shapeB + " must match"));
- }
- function assertTypesMatch(a, b) {
- assert(a.dtype === b.dtype, " The dtypes of the first(" + a.dtype + ") and" +
- (" second(" + b.dtype + ") input must match"));
- }
- function flatten(arr, ret) {
- if (ret === void 0) { ret = []; }
- if (Array.isArray(arr)) {
- for (var i = 0; i < arr.length; ++i) {
- flatten(arr[i], ret);
- }
- }
- else {
- ret.push(arr);
- }
- return ret;
- }
- function inferShape(val) {
- if (isTypedArray(val)) {
- return [val.length];
- }
- if (!Array.isArray(val)) {
- return [];
- }
- var shape = [];
- while (val instanceof Array) {
- shape.push(val.length);
- val = val[0];
- }
- return shape;
- }
- function sizeFromShape(shape) {
- if (shape.length === 0) {
- return 1;
- }
- var size = shape[0];
- for (var i = 1; i < shape.length; i++) {
- size *= shape[i];
- }
- return size;
- }
- function isScalarShape(shape) {
- return shape.length === 0;
- }
- function arraysEqual(n1, n2) {
- if (n1.length !== n2.length) {
- return false;
- }
- for (var i = 0; i < n1.length; i++) {
- if (n1[i] !== n2[i]) {
- return false;
- }
- }
- return true;
- }
- function isInt(a) {
- return a % 1 === 0;
- }
- function tanh(x) {
- if (Math.tanh != null) {
- return Math.tanh(x);
- }
- if (x === Infinity) {
- return 1;
- }
- else if (x === -Infinity) {
- return -1;
- }
- else {
- var e2x = Math.exp(2 * x);
- return (e2x - 1) / (e2x + 1);
- }
- }
- function sizeToSquarishShape(size) {
- for (var a = Math.floor(Math.sqrt(size)); a > 1; --a) {
- if (size % a === 0) {
- return [a, size / a];
- }
- }
- return [1, size];
- }
- function createShuffledIndices(n) {
- var shuffledIndices = new Uint32Array(n);
- for (var i = 0; i < n; ++i) {
- shuffledIndices[i] = i;
- }
- shuffle(shuffledIndices);
- return shuffledIndices;
- }
- function rightPad(a, size) {
- if (size <= a.length) {
- return a;
- }
- return a + ' '.repeat(size - a.length);
- }
- function repeatedTry(checkFn, delayFn, maxCounter) {
- if (delayFn === void 0) { delayFn = function (counter) { return 0; }; }
- return new Promise(function (resolve, reject) {
- var tryCount = 0;
- var tryFn = function () {
- if (checkFn()) {
- resolve();
- return;
- }
- tryCount++;
- var nextBackoff = delayFn(tryCount);
- if (maxCounter != null && tryCount >= maxCounter) {
- reject();
- return;
- }
- setTimeout(tryFn, nextBackoff);
- };
- setTimeout(tryFn, 0);
- });
- }
- function getQueryParams(queryString) {
- var params = {};
- queryString.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g, function (s) {
- var t = [];
- for (var _i = 1; _i < arguments.length; _i++) {
- t[_i - 1] = arguments[_i];
- }
- decodeParam(params, t[0], t[1]);
- return t.join('=');
- });
- return params;
- }
- function decodeParam(params, name, value) {
- params[decodeURIComponent(name)] = decodeURIComponent(value || '');
- }
- function inferFromImplicitShape(shape, size) {
- var shapeProd = 1;
- var implicitIdx = -1;
- for (var i = 0; i < shape.length; ++i) {
- if (shape[i] > 0) {
- shapeProd *= shape[i];
- }
- else if (shape[i] === -1) {
- if (implicitIdx !== -1) {
- throw Error("Shapes can only have 1 implicit size. " +
- ("Found - 1 at dim " + implicitIdx + " and dim " + i));
- }
- implicitIdx = i;
- }
- else if (shape[i] <= 0) {
- throw Error("Shapes can not be <= 0. Found " + shape[i] + " at dim " + i);
- }
- }
- if (implicitIdx === -1) {
- if (size > 0 && size !== shapeProd) {
- throw Error("Size(" + size + ") must match the product of shape " + shape);
- }
- return shape;
- }
- if (size % shapeProd !== 0) {
- throw Error("The implicit shape can't be a fractional number. " +
- ("Got " + size + " / " + shapeProd));
- }
- var newShape = shape.slice();
- newShape[implicitIdx] = size / shapeProd;
- return newShape;
- }
- function squeezeShape(shape, axis) {
- var newShape = [];
- var keptDims = [];
- var j = 0;
- for (var i = 0; i < shape.length; ++i) {
- if (axis != null) {
- if (axis[j] === i && shape[i] > 1) {
- throw new Error("Can't squeeze axis " + i + " since its dim '" + shape[i] + "' is not 1");
- }
- if ((axis[j] == null || axis[j] > i) && shape[i] === 1) {
- newShape.push(shape[i]);
- keptDims.push(i);
- }
- if (axis[j] <= i) {
- j++;
- }
- }
- if (shape[i] > 1) {
- newShape.push(shape[i]);
- keptDims.push(i);
- }
- }
- return { newShape: newShape, keptDims: keptDims };
- }
- function getTypedArrayFromDType(dtype, size) {
- var values = null;
- if (dtype == null || dtype === 'float32') {
- values = new Float32Array(size);
- }
- else if (dtype === 'int32') {
- values = new Int32Array(size);
- }
- else if (dtype === 'bool') {
- values = new Uint8Array(size);
- }
- else {
- throw new Error("Unknown data type " + dtype);
- }
- return values;
- }
- function isTensorInList(tensor, tensorList) {
- for (var i = 0; i < tensorList.length; i++) {
- if (tensorList[i].id === tensor.id) {
- return true;
- }
- }
- return false;
- }
- function checkForNaN(vals, dtype, name) {
- if (dtype !== 'float32') {
- return;
- }
- for (var i = 0; i < vals.length; i++) {
- if (isNaN(vals[i])) {
- throw Error("The result of the '" + name + "' has NaNs.");
- }
- }
- }
- function flattenNameArrayMap(nameArrayMap, keys) {
- var xs = [];
- if (nameArrayMap instanceof Tensor) {
- xs.push(nameArrayMap);
- }
- else {
- var xMap = nameArrayMap;
- for (var i = 0; i < keys.length; i++) {
- xs.push(xMap[keys[i]]);
- }
- }
- return xs;
- }
- function unflattenToNameArrayMap(keys, flatArrays) {
- if (keys.length !== flatArrays.length) {
- throw new Error("Cannot unflatten Tensor[], keys and arrays are not of same length.");
- }
- var result = {};
- for (var i = 0; i < keys.length; i++) {
- result[keys[i]] = flatArrays[i];
- }
- return result;
- }
- function hasEncodingLoss(oldType, newType) {
- if (newType === 'float32') {
- return false;
- }
- if (newType === 'int32' && oldType !== 'float32') {
- return false;
- }
- if (newType === 'bool' && oldType === 'bool') {
- return false;
- }
- return true;
- }
- function copyTypedArray(array, dtype) {
- if (dtype == null || dtype === 'float32') {
- return new Float32Array(array);
- }
- else if (dtype === 'int32') {
- return new Int32Array(array);
- }
- else if (dtype === 'bool') {
- var bool = new Uint8Array(array.length);
- for (var i = 0; i < bool.length; ++i) {
- if (Math.round(array[i]) !== 0) {
- bool[i] = 1;
- }
- }
- return bool;
- }
- else {
- throw new Error("Unknown data type " + dtype);
- }
- }
- function isTypedArray(a) {
- return a instanceof Float32Array || a instanceof Int32Array ||
- a instanceof Uint8Array;
- }
- function bytesPerElement(dtype) {
- if (dtype === 'float32' || dtype === 'int32') {
- return 4;
- }
- else if (dtype === 'bool') {
- return 1;
- }
- else {
- throw new Error("Unknown dtype " + dtype);
- }
- }
- function isFunction(f) {
- return !!(f && f.constructor && f.call && f.apply);
- }
- function extractTensorsFromContainer(result) {
- return extractTensorsFromAny(result);
- }
- function extractTensorsFromAny(result) {
- if (result == null) {
- return [];
- }
- if (result instanceof Tensor) {
- return [result];
- }
- var list = [];
- var resultObj = result;
- if (!isIterable(resultObj)) {
- return [];
- }
- for (var k in resultObj) {
- var sublist = flatten(resultObj[k]).filter(function (x) { return x instanceof Tensor; });
- list.push.apply(list, sublist);
- }
- return list;
- }
- function isIterable(obj) {
- return Array.isArray(obj) || typeof obj === 'object';
- }
-
- var util = /*#__PURE__*/Object.freeze({
- assertArgumentsAreTensors: assertArgumentsAreTensors,
- shuffle: shuffle,
- clamp: clamp,
- randUniform: randUniform,
- distSquared: distSquared,
- assert: assert,
- assertShapesMatch: assertShapesMatch,
- assertTypesMatch: assertTypesMatch,
- flatten: flatten,
- inferShape: inferShape,
- sizeFromShape: sizeFromShape,
- isScalarShape: isScalarShape,
- arraysEqual: arraysEqual,
- isInt: isInt,
- tanh: tanh,
- sizeToSquarishShape: sizeToSquarishShape,
- createShuffledIndices: createShuffledIndices,
- rightPad: rightPad,
- repeatedTry: repeatedTry,
- getQueryParams: getQueryParams,
- inferFromImplicitShape: inferFromImplicitShape,
- squeezeShape: squeezeShape,
- getTypedArrayFromDType: getTypedArrayFromDType,
- isTensorInList: isTensorInList,
- checkForNaN: checkForNaN,
- flattenNameArrayMap: flattenNameArrayMap,
- unflattenToNameArrayMap: unflattenToNameArrayMap,
- hasEncodingLoss: hasEncodingLoss,
- copyTypedArray: copyTypedArray,
- isTypedArray: isTypedArray,
- bytesPerElement: bytesPerElement,
- isFunction: isFunction,
- extractTensorsFromContainer: extractTensorsFromContainer,
- extractTensorsFromAny: extractTensorsFromAny
- });
-
- var FORMAT_LIMIT_NUM_VALS = 20;
- var FORMAT_NUM_FIRST_LAST_VALS = 3;
- var FORMAT_NUM_SIG_DIGITS = 7;
- function tensorToString(t, verbose) {
- var vals = t.dataSync();
- var padPerCol = computeMaxSizePerColumn(t);
- var valsLines = subTensorToString(vals, t.shape, t.strides, padPerCol);
- var lines = ['Tensor'];
- if (verbose) {
- lines.push(" dtype: " + t.dtype);
- lines.push(" rank: " + t.rank);
- lines.push(" shape: [" + t.shape + "]");
- lines.push(" values:");
- }
- lines.push(valsLines.map(function (l) { return ' ' + l; }).join('\n'));
- return lines.join('\n');
- }
- function computeMaxSizePerColumn(t) {
- var vals = t.dataSync();
- var n = t.size;
- var numCols = t.strides[t.strides.length - 1];
- var padPerCol = new Array(numCols).fill(0);
- if (t.rank > 1) {
- for (var row = 0; row < n / numCols; row++) {
- var offset = row * numCols;
- for (var j = 0; j < numCols; j++) {
- padPerCol[j] =
- Math.max(padPerCol[j], valToString(vals[offset + j], 0).length);
- }
- }
- }
- return padPerCol;
- }
- function valToString(val, pad) {
- return rightPad(parseFloat(val.toFixed(FORMAT_NUM_SIG_DIGITS)).toString(), pad);
- }
- function subTensorToString(vals, shape, strides, padPerCol, isLast) {
- if (isLast === void 0) { isLast = true; }
- var size = shape[0];
- var rank = shape.length;
- if (rank === 0) {
- return [vals[0].toString()];
- }
- if (rank === 1) {
- if (size > FORMAT_LIMIT_NUM_VALS) {
- var firstVals = Array.from(vals.subarray(0, FORMAT_NUM_FIRST_LAST_VALS));
- var lastVals = Array.from(vals.subarray(size - FORMAT_NUM_FIRST_LAST_VALS, size));
- return [
- '[' + firstVals.map(function (x, i) { return valToString(x, padPerCol[i]); }).join(', ') +
- ', ..., ' +
- lastVals
- .map(function (x, i) { return valToString(x, padPerCol[size - FORMAT_NUM_FIRST_LAST_VALS + i]); })
- .join(', ') +
- ']'
- ];
- }
- return [
- '[' +
- Array.from(vals).map(function (x, i) { return valToString(x, padPerCol[i]); }).join(', ') +
- ']'
- ];
- }
- var subshape = shape.slice(1);
- var substrides = strides.slice(1);
- var stride = strides[0];
- var lines = [];
- if (size > FORMAT_LIMIT_NUM_VALS) {
- for (var i = 0; i < FORMAT_NUM_FIRST_LAST_VALS; i++) {
- var start = i * stride;
- var end = start + stride;
- lines.push.apply(lines, subTensorToString(vals.subarray(start, end), subshape, substrides, padPerCol, false));
- }
- lines.push('...');
- for (var i = size - FORMAT_NUM_FIRST_LAST_VALS; i < size; i++) {
- var start = i * stride;
- var end = start + stride;
- lines.push.apply(lines, subTensorToString(vals.subarray(start, end), subshape, substrides, padPerCol, i === size - 1));
- }
- }
- else {
- for (var i = 0; i < size; i++) {
- var start = i * stride;
- var end = start + stride;
- lines.push.apply(lines, subTensorToString(vals.subarray(start, end), subshape, substrides, padPerCol, i === size - 1));
- }
- }
- var sep = rank === 2 ? ',' : '';
- lines[0] = '[' + lines[0] + sep;
- for (var i = 1; i < lines.length - 1; i++) {
- lines[i] = ' ' + lines[i] + sep;
- }
- var newLineSep = ',\n';
- for (var i = 2; i < rank; i++) {
- newLineSep += '\n';
- }
- lines[lines.length - 1] =
- ' ' + lines[lines.length - 1] + ']' + (isLast ? '' : newLineSep);
- return lines;
- }
-
- function axesAreInnerMostDims(axes, rank) {
- for (var i = 0; i < axes.length; ++i) {
- if (axes[axes.length - i - 1] !== rank - 1 - i) {
- return false;
- }
- }
- return true;
- }
- function combineLocations(outputLoc, reduceLoc, axes) {
- var rank = outputLoc.length + reduceLoc.length;
- var loc = [];
- var outIdx = 0;
- var reduceIdx = 0;
- for (var dim = 0; dim < rank; dim++) {
- if (axes.indexOf(dim) === -1) {
- loc.push(outputLoc[outIdx++]);
- }
- else {
- loc.push(reduceLoc[reduceIdx++]);
- }
- }
- return loc;
- }
- function computeOutAndReduceShapes(aShape, axes) {
- var outShape = [];
- var rank = aShape.length;
- for (var dim = 0; dim < rank; dim++) {
- if (axes.indexOf(dim) === -1) {
- outShape.push(aShape[dim]);
- }
- }
- var reduceShape = axes.map(function (dim) { return aShape[dim]; });
- return [outShape, reduceShape];
- }
- function expandShapeToKeepDim(shape, axes) {
- var reduceSubShape = axes.map(function (x) { return 1; });
- return combineLocations(shape, reduceSubShape, axes);
- }
- function parseAxisParam(axis, shape) {
- var rank = shape.length;
- axis = axis == null ? shape.map(function (s, i) { return i; }) : [].concat(axis);
- assert(axis.every(function (ax) { return ax >= -rank && ax < rank; }), "All values in axis param must be in range [-" + rank + ", " + rank + ") but " +
- ("got axis " + axis));
- assert(axis.every(function (ax) { return isInt(ax); }), "All values in axis param must be integers but " +
- ("got axis " + axis));
- return axis.map(function (a) { return a < 0 ? rank + a : a; });
- }
- function assertAxesAreInnerMostDims(msg, axes, rank) {
- assert(axesAreInnerMostDims(axes, rank), msg + " supports only inner-most axes for now. " +
- ("Got axes " + axes + " and rank-" + rank + " input."));
- }
- function getAxesPermutation(axes, rank) {
- if (axesAreInnerMostDims(axes, rank)) {
- return null;
- }
- var result = [];
- for (var i = 0; i < rank; ++i) {
- if (axes.indexOf(i) === -1) {
- result.push(i);
- }
- }
- axes.forEach(function (axis) { return result.push(axis); });
- return result;
- }
- function getUndoAxesPermutation(axes) {
- return axes.map(function (axis, i) { return [i, axis]; })
- .sort(function (a, b) { return a[1] - b[1]; })
- .map(function (x) { return x[0]; });
- }
- function getInnerMostAxes(numAxes, rank) {
- var res = [];
- for (var i = rank - numAxes; i < rank; ++i) {
- res.push(i);
- }
- return res;
- }
-
- function assertParams(aShape, bShape, axis) {
- var aRank = aShape.length;
- var bRank = bShape.length;
- assert(aShape.length === bShape.length, "Error in concat" + aRank + "D: rank of x1 (" + aRank + ") and x2 (" + bRank + ") " +
- "must be the same.");
- assert(axis >= 0 && axis < aRank, "Error in concat" + aRank + "D: axis must be " +
- ("between 0 and " + (aRank - 1) + "."));
- for (var i = 0; i < aRank; i++) {
- assert((i === axis) || (aShape[i] === bShape[i]), "Error in concat" + aRank + "D: Shape (" + aShape + ") does not match " +
- ("(" + bShape + ") along the non-concatenated axis " + i + "."));
- }
- }
- function computeOutShape(x1Shape, x2Shape, axis) {
- assert(x1Shape.length === x2Shape.length, 'x1 and x2 should have the same rank.');
- var outputShape = x1Shape.slice();
- outputShape[axis] += x2Shape[axis];
- return outputShape;
- }
- function computeGradientSliceShapes(aShape, bShape) {
- return {
- aBegin: [0, 0],
- aSize: aShape,
- bBegin: [0, aShape[1]],
- bSize: bShape
- };
- }
-
- function operation(target, name, descriptor) {
- var fn = descriptor.value;
- descriptor.value = function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return tidy(name, function () { return fn.apply(void 0, args); });
- };
- return descriptor;
- }
-
- var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var ConcatOps = (function () {
- function ConcatOps() {
- }
- ConcatOps.concat1d = function (tensors) {
- return ConcatOps.concat(tensors, 0);
- };
- ConcatOps.concat2d = function (tensors, axis) {
- return ConcatOps.concat(tensors, axis);
- };
- ConcatOps.concat3d = function (tensors, axis) {
- return ConcatOps.concat(tensors, axis);
- };
- ConcatOps.concat4d = function (tensors, axis) {
- return ConcatOps.concat(tensors, axis);
- };
- ConcatOps.concat = function (tensors, axis) {
- if (axis === void 0) { axis = 0; }
- assert(tensors.length >= 1, 'Pass at least one tensor to concat');
- assertArgumentsAreTensors({ tensors: tensors }, 'concat');
- var result = tensors[0];
- if (tensors.length === 1) {
- return result;
- }
- var axes = parseAxisParam(axis, result.shape);
- for (var i = 1; i < tensors.length; ++i) {
- result = concat2Tensors(result, tensors[i], axes[0]);
- }
- return result;
- };
- __decorate([
- doc({ heading: 'Tensors', subheading: 'Slicing and Joining' }),
- operation
- ], ConcatOps, "concat", null);
- return ConcatOps;
- }());
- function concat2Tensors(a, b, axis) {
- assertParams(a.shape, b.shape, axis);
- var outShape = computeOutShape(a.shape, b.shape, axis);
- var a2D = a.as2D(-1, sizeFromShape(a.shape.slice(axis)));
- var b2D = b.as2D(-1, sizeFromShape(b.shape.slice(axis)));
- var _a = computeGradientSliceShapes(a2D.shape, b2D.shape), aBegin = _a.aBegin, aSize = _a.aSize, bBegin = _a.bBegin, bSize = _a.bSize;
- var der = function (dy) {
- return { a: function () { return dy.slice(aBegin, aSize); }, b: function () { return dy.slice(bBegin, bSize); } };
- };
- var res = ENV.engine.runKernel(function (backend) { return backend.concat(a2D, b2D); }, { a: a2D, b: b2D }, der);
- return res.reshape(outShape);
- }
-
- var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
-
- function createCommonjsModule(fn, module) {
- return module = { exports: {} }, fn(module, module.exports), module.exports;
- }
-
- var alea = createCommonjsModule(function (module) {
- // A port of an algorithm by Johannes Baagøe , 2010
- // http://baagoe.com/en/RandomMusings/javascript/
- // https://github.com/nquinlan/better-random-numbers-for-javascript-mirror
- // Original work is under MIT license -
-
- // Copyright (C) 2010 by Johannes Baagøe
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
-
-
-
- (function(global, module, define) {
-
- function Alea(seed) {
- var me = this, mash = Mash();
-
- me.next = function() {
- var t = 2091639 * me.s0 + me.c * 2.3283064365386963e-10; // 2^-32
- me.s0 = me.s1;
- me.s1 = me.s2;
- return me.s2 = t - (me.c = t | 0);
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
+ (factory((global.faceapi = global.faceapi || {})));
+}(this, (function (exports) { 'use strict';
+
+ /**
+ * @license
+ * Copyright 2018 Google LLC. All Rights Reserved.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * =============================================================================
+ */
+ var extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t;}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);};function __extends(e,t){function n(){this.constructor=e;}extendStatics(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n);}function __decorate(e,t,n,r){var a,o=arguments.length,i=o<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(a=e[s])&&(i=(o<3?a(i):o>3?a(t,n,i):a(t,n))||i);return o>3&&i&&Object.defineProperty(t,n,i),i}function __awaiter(e,t,n,r){return new(n||(n=Promise))(function(a,o){function i(e){try{u(r.next(e));}catch(e){o(e);}}function s(e){try{u(r.throw(e));}catch(e){o(e);}}function u(e){e.done?a(e.value):new n(function(t){t(e.value);}).then(i,s);}u((r=r.apply(e,t||[])).next());})}function __generator(e,t){var n,r,a,o,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;i;)try{if(n=1,r&&(a=2&o[0]?r.return:o[0]?r.throw||((a=r.return)&&a.call(r),0):r.next)&&!(a=a.call(r,o[1])).done)return a;switch(r=0,a&&(o=[2&o[0],a.value]),o[0]){case 0:case 1:a=o;break;case 4:return i.label++,{value:o[1],done:!1};case 5:i.label++,r=o[1],o=[0];continue;case 7:o=i.ops.pop(),i.trys.pop();continue;default:if(!(a=(a=i.trys).length>0&&a[a.length-1])&&(6===o[0]||2===o[0])){i=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]0;)r=Math.random()*t|0,n=e[--t],e[t]=e[r],e[r]=n;}function clamp(e,t,n){return Math.max(e,Math.min(t,n))}function randUniform(e,t){return Math.random()*(t-e)+e}function distSquared(e,t){for(var n=0,r=0;r1;--t)if(e%t==0)return [t,e/t];return [1,e]}function createShuffledIndices(e){for(var t=new Uint32Array(e),n=0;n=n?a():setTimeout(i,s);}};i();})}function getQueryParams(e){var t={};return e.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,function(e){for(var n=[],r=1;r0)n*=e[a];else if(-1===e[a]){if(-1!==r)throw Error("Shapes can only have 1 implicit size. Found - 1 at dim "+r+" and dim "+a);r=a;}else if(e[a]<=0)throw Error("Shapes can not be <= 0. Found "+e[a]+" at dim "+a);if(-1===r){if(t>0&&t!==n)throw Error("Size("+t+") must match the product of shape "+e);return e}if(t%n!=0)throw Error("The implicit shape can't be a fractional number. Got "+t+" / "+n);var o=e.slice();return o[r]=t/n,o}function squeezeShape(e,t){for(var n=[],r=[],a=0,o=0;o1)throw new Error("Can't squeeze axis "+o+" since its dim '"+e[o]+"' is not 1");(null==t[a]||t[a]>o)&&1===e[o]&&(n.push(e[o]),r.push(o)),t[a]<=o&&a++;}e[o]>1&&(n.push(e[o]),r.push(o));}return {newShape:n,keptDims:r}}function getTypedArrayFromDType(e,t){var n=null;if(null==e||"float32"===e)n=new Float32Array(t);else if("int32"===e)n=new Int32Array(t);else{if("bool"!==e)throw new Error("Unknown data type "+e);n=new Uint8Array(t);}return n}function isTensorInList(e,t){for(var n=0;n1)for(var o=0;oFORMAT_LIMIT_NUM_VALS){var s=Array.from(e.subarray(0,FORMAT_NUM_FIRST_LAST_VALS)),u=Array.from(e.subarray(o-FORMAT_NUM_FIRST_LAST_VALS,o));return ["["+s.map(function(e,t){return valToString(e,r[t])}).join(", ")+", ..., "+u.map(function(e,t){return valToString(e,r[o-FORMAT_NUM_FIRST_LAST_VALS+t])}).join(", ")+"]"]}return ["["+Array.from(e).map(function(e,t){return valToString(e,r[t])}).join(", ")+"]"]}var l=t.slice(1),c=n.slice(1),d=n[0],p=[];if(o>FORMAT_LIMIT_NUM_VALS){for(var h=0;h=-n&&e=0&&n=1,"Pass at least one tensor to concat"),assertArgumentsAreTensors({tensors:e},"concat");var n=e[0];if(1===e.length)return n;for(var r=parseAxisParam(t,n.shape),a=1;a>>0,t=(r*=t)>>>0,t+=4294967296*(r-=t);}return 2.3283064365386963e-10*(t>>>0)});n.next=function(){var e=2091639*n.s0+2.3283064365386963e-10*n.c;return n.s0=n.s1,n.s1=n.s2,n.s2=e-(n.c=0|e)},n.c=1,n.s0=r(" "),n.s1=r(" "),n.s2=r(" "),n.s0-=r(e),n.s0<0&&(n.s0+=1),n.s1-=r(e),n.s1<0&&(n.s1+=1),n.s2-=r(e),n.s2<0&&(n.s2+=1),r=null;}(e),a=t&&t.state,o=n.next;return o.int32=function(){return 4294967296*n.next()|0},o.double=function(){return o()+1.1102230246251565e-16*(2097152*o()|0)},o.quick=o,a&&("object"==typeof a&&r(a,n),o.state=function(){return r(n,{})}),o}t&&t.exports?t.exports=a:n&&n.amd?n(function(){return a}):this.alea=a;}(0,e,!1);}),xor128=createCommonjsModule(function(e){!function(e,t,n){function r(e,t){return t.x=e.x,t.y=e.y,t.z=e.z,t.w=e.w,t}function a(e,t){var n=new function(e){var t=this,n="";t.x=0,t.y=0,t.z=0,t.w=0,t.next=function(){var e=t.x^t.x<<11;return t.x=t.y,t.y=t.z,t.z=t.w,t.w^=t.w>>>19^e^e>>>8},e===(0|e)?t.x=e:n+=e;for(var r=0;r>>0)/4294967296};return o.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21);}while(0===e);return e},o.int32=n.next,o.quick=o,a&&("object"==typeof a&&r(a,n),o.state=function(){return r(n,{})}),o}t&&t.exports?t.exports=a:n&&n.amd?n(function(){return a}):this.xor128=a;}(0,e,!1);}),xorwow=createCommonjsModule(function(e){!function(e,t,n){function r(e,t){return t.x=e.x,t.y=e.y,t.z=e.z,t.w=e.w,t.v=e.v,t.d=e.d,t}function a(e,t){var n=new function(e){var t=this,n="";t.next=function(){var e=t.x^t.x>>>2;return t.x=t.y,t.y=t.z,t.z=t.w,t.w=t.v,(t.d=t.d+362437|0)+(t.v=t.v^t.v<<4^e^e<<1)|0},t.x=0,t.y=0,t.z=0,t.w=0,t.v=0,e===(0|e)?t.x=e:n+=e;for(var r=0;r>>4),t.next();}(e),a=t&&t.state,o=function(){return (n.next()>>>0)/4294967296};return o.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21);}while(0===e);return e},o.int32=n.next,o.quick=o,a&&("object"==typeof a&&r(a,n),o.state=function(){return r(n,{})}),o}t&&t.exports?t.exports=a:n&&n.amd?n(function(){return a}):this.xorwow=a;}(0,e,!1);}),xorshift7=createCommonjsModule(function(e){!function(e,t,n){function r(e,t){return t.x=e.x.slice(),t.i=e.i,t}function a(e,t){null==e&&(e=+new Date);var n=new function(e){var t=this;t.next=function(){var e,n,r=t.x,a=t.i;return e=r[a],n=(e^=e>>>7)^e<<24,n^=(e=r[a+1&7])^e>>>10,n^=(e=r[a+3&7])^e>>>3,n^=(e=r[a+4&7])^e<<7,e=r[a+7&7],n^=(e^=e<<13)^e<<9,r[a]=n,t.i=a+1&7,n},function(e,t){var n,r=[];if(t===(0|t))r[0]=t;else for(t=""+t,n=0;n0;--n)e.next();}(t,e);}(e),a=t&&t.state,o=function(){return (n.next()>>>0)/4294967296};return o.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21);}while(0===e);return e},o.int32=n.next,o.quick=o,a&&(a.x&&r(a,n),o.state=function(){return r(n,{})}),o}t&&t.exports?t.exports=a:n&&n.amd?n(function(){return a}):this.xorshift7=a;}(0,e,!1);}),xor4096=createCommonjsModule(function(e){!function(e,t,n){function r(e,t){return t.i=e.i,t.w=e.w,t.X=e.X.slice(),t}function a(e,t){null==e&&(e=+new Date);var n=new function(e){var t=this;t.next=function(){var e,n,r=t.w,a=t.X,o=t.i;return t.w=r=r+1640531527|0,n=a[o+34&127],e=a[o=o+1&127],n^=n<<13,e^=e<<17,n^=n>>>15,e^=e>>>12,n=a[o]=n^e,t.i=o,n+(r^r>>>16)|0},function(e,t){var n,r,a,o,i,s=[],u=128;for(t===(0|t)?(r=t,t=null):(t+="\0",r=0,u=Math.max(u,t.length)),a=0,o=-32;o>>15,r^=r<<4,r^=r>>>13,o>=0&&(i=i+1640531527|0,a=0==(n=s[127&o]^=r+i)?a+1:0);for(a>=128&&(s[127&(t&&t.length||0)]=-1),a=127,o=512;o>0;--o)r=s[a+34&127],n=s[a=a+1&127],r^=r<<13,n^=n<<17,r^=r>>>15,n^=n>>>12,s[a]=r^n;e.w=i,e.X=s,e.i=a;}(t,e);}(e),a=t&&t.state,o=function(){return (n.next()>>>0)/4294967296};return o.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21);}while(0===e);return e},o.int32=n.next,o.quick=o,a&&(a.X&&r(a,n),o.state=function(){return r(n,{})}),o}t&&t.exports?t.exports=a:n&&n.amd?n(function(){return a}):this.xor4096=a;}(0,e,!1);}),tychei=createCommonjsModule(function(e){!function(e,t,n){function r(e,t){return t.a=e.a,t.b=e.b,t.c=e.c,t.d=e.d,t}function a(e,t){var n=new function(e){var t=this,n="";t.next=function(){var e=t.b,n=t.c,r=t.d,a=t.a;return e=e<<25^e>>>7^n,n=n-r|0,r=r<<24^r>>>8^a,a=a-e|0,t.b=e=e<<20^e>>>12^n,t.c=n=n-r|0,t.d=r<<16^n>>>16^a,t.a=a-e|0},t.a=0,t.b=0,t.c=-1640531527,t.d=1367130551,e===Math.floor(e)?(t.a=e/4294967296|0,t.b=0|e):n+=e;for(var r=0;r>>0)/4294967296};return o.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21);}while(0===e);return e},o.int32=n.next,o.quick=o,a&&("object"==typeof a&&r(a,n),o.state=function(){return r(n,{})}),o}t&&t.exports?t.exports=a:n&&n.amd?n(function(){return a}):this.tychei=a;}(0,e,!1);}),seedrandom=createCommonjsModule(function(e){!function(t,n){var r,a=this,o=256,i=6,s="random",u=n.pow(o,i),l=n.pow(2,52),c=2*l,d=o-1;function p(e,p,m){var v=[],y=f(function e(t,n){var r,a=[],o=typeof t;if(n&&"object"==o)for(r in t)try{a.push(e(t[r],n-1));}catch(e){}return a.length?a:"string"==o?t:t+"\0"}((p=1==p?{entropy:!0}:p||{}).entropy?[e,g(t)]:null==e?function(){try{var e;return r&&(e=r.randomBytes)?e=e(o):(e=new Uint8Array(o),(a.crypto||a.msCrypto).getRandomValues(e)),g(e)}catch(e){var n=a.navigator,i=n&&n.plugins;return [+new Date,a,i,a.screen,g(t)]}}():e,3),v),x=new function(e){var t,n=e.length,r=this,a=0,i=r.i=r.j=0,s=r.S=[];n||(e=[n++]);for(;a=c;)e/=2,t/=2,n>>>=1;return (e+n)/t};return b.int32=function(){return 0|x.g(4)},b.quick=function(){return x.g(4)/4294967296},b.double=b,f(g(x.S),t),(p.pass||m||function(e,t,r,a){return a&&(a.S&&h(a,x),e.state=function(){return h(x,{})}),r?(n[s]=e,t):e})(b,y,"global"in p?p.global:this==n,p.state)}function h(e,t){return t.i=e.i,t.j=e.j,t.S=e.S.slice(),t}function f(e,t){for(var n,r=e+"",a=0;a=1||0===i);var s=Math.sqrt(-2*Math.log(i)/i);t=this.mean+this.stdDev*a*s,n=this.mean+this.stdDev*o*s,this.truncated&&!this.isValidTruncated(t)||(r=!0);}return this.truncated&&!this.isValidTruncated(n)||(this.nextVal=this.convertValue(n)),this.convertValue(t)},e.prototype.convertValue=function(e){return null==this.dtype||"float32"===this.dtype?e:Math.round(e)},e.prototype.isValidTruncated=function(e){return e<=this.upper&&e>=this.lower},e}();!function(e){e.float32="float32",e.int32="int32",e.bool="bool";}(DType||(DType={})),function(e){e.R0="R0",e.R1="R1",e.R2="R2",e.R3="R3",e.R4="R4",e.R5="R5",e.R6="R6";}(Rank||(Rank={})),function(e){e.float32="float32",e.int32="int32",e.bool="int32";}(UpcastInt32AndMap||(UpcastInt32AndMap={})),function(e){e.float32="float32",e.int32="int32",e.bool="bool";}(UpcastBoolAndMap||(UpcastBoolAndMap={})),function(e){e.float32="float32",e.int32="float32",e.bool="float32";}(UpcastFloat32AndMap||(UpcastFloat32AndMap={}));var upcastTypeMap={float32:UpcastFloat32AndMap,int32:UpcastInt32AndMap,bool:UpcastBoolAndMap};function upcastType(e,t){return upcastTypeMap[e][t]}function sumOutType(e){return upcastType(e,"int32")}function getBroadcastDims(e,t){for(var n=e.length,r=[],a=0;a1&&1===i&&r.unshift(o);}return r}function getReductionAxes(e,t){for(var n=[],r=0;r1)&&n.unshift(o);}return n}function broadcastDimsAreOuter(e){for(var t=0;t1&&s>1&&i!==s)throw Error(r);n.unshift(Math.max(i,s));}return n}var BinaryOps=function(){function e(){}return e.add=function(e,t){assertArgumentsAreTensors({a:e,b:t},"add"),assertTypesMatch(e,t);var n=assertAndGetBroadcastShape(e.shape,t.shape);return ENV.engine.runKernel(function(n){return n.add(e,t)},{a:e,b:t},function(r){return {a:function(){var t=r,a=getReductionAxes(e.shape,n);return a.length>0&&(t=t.sum(a)),t.reshape(e.shape)},b:function(){var e=r,a=getReductionAxes(t.shape,n);return a.length>0&&(e=e.sum(a)),e.reshape(t.shape)}}})},e.addStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in addStrict: "),e.add(t)},e.sub=function(e,t){assertArgumentsAreTensors({a:e,b:t},"sub"),assertTypesMatch(e,t);var n=assertAndGetBroadcastShape(e.shape,t.shape);return ENV.engine.runKernel(function(n){return n.subtract(e,t)},{a:e,b:t},function(r){return {a:function(){var t=r,a=getReductionAxes(e.shape,n);return a.length>0&&(t=t.sum(a)),t.reshape(e.shape)},b:function(){var e=r,a=getReductionAxes(t.shape,n);return a.length>0&&(e=e.sum(a)),e.neg().reshape(t.shape)}}})},e.subStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in subStrict: "),e.sub(t)},e.pow=function(e,t){assertArgumentsAreTensors({base:e,exp:t},"pow");var n=assertAndGetBroadcastShape(e.shape,t.shape);e=e.cast(upcastType(e.dtype,t.dtype)),t=t.cast(upcastType(e.dtype,t.dtype));return ENV.engine.runKernel(function(n,r){return r(n.pow(e,t))},{base:e,exp:t},function(r,a){var o=a[0];return {base:function(){var a=r.mul(t.toFloat().mul(o.div(e))),i=getReductionAxes(e.shape,n);return i.length>0&&(a=a.sum(i)),a.reshape(e.shape)},exp:function(){var a=r.mul(o.mul(e.log()).toFloat()),i=getReductionAxes(t.shape,n);return i.length>0&&(a=a.sum(i)),a.reshape(t.shape)}}})},e.powStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in powStrict: "),e.pow(t)},e.mul=function(e,t){assertArgumentsAreTensors({a:e,b:t},"mul"),assertTypesMatch(e,t);var n=assertAndGetBroadcastShape(e.shape,t.shape);return ENV.engine.runKernel(function(n){return n.multiply(e,t)},{a:e,b:t},function(r){return {a:function(){var a=r.mul(t.toFloat()),o=getReductionAxes(e.shape,n);return o.length>0?a.sum(o).reshape(e.shape):a},b:function(){var a=r.mul(e.toFloat()),o=getReductionAxes(t.shape,n);return o.length>0?a.sum(o).reshape(t.shape):a}}})},e.mulStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in multiplyStrict: "),e.mul(t)},e.div=function(t,n){var r;if(assertArgumentsAreTensors({a:t,b:n},"div"),assertTypesMatch(t,n),"int32"===t.dtype&&"int32"===n.dtype)return e.floorDiv(t,n);r=function(e){return e.realDivide(t,n)};var a=assertAndGetBroadcastShape(t.shape,n.shape);return ENV.engine.runKernel(r,{a:t,b:n},function(e){return {a:function(){var r=e.div(n.toFloat()),o=getReductionAxes(t.shape,a);return o.length>0?r.sum(o).reshape(t.shape):r},b:function(){var r=e.mul(t.toFloat()),o=getReductionAxes(n.shape,a);o.length>0&&(r=r.sum(o).reshape(n.shape));var i=n.square();return r.div(i.toFloat()).neg()}}})},e.floorDiv=function(e,t){assertArgumentsAreTensors({a:e,b:t},"floorDiv"),assertTypesMatch(e,t);var n=assertAndGetBroadcastShape(e.shape,t.shape);return ENV.engine.runKernel(function(n){return n.floorDiv(e,t)},{a:e,b:t},function(r){return {a:function(){var a=r.div(t.toFloat()),o=getReductionAxes(e.shape,n);return o.length>0?a.sum(o).reshape(e.shape):a},b:function(){var a=r.mul(e.toFloat()),o=getReductionAxes(t.shape,n);o.length>0&&(a=a.sum(o).reshape(t.shape));var i=t.square();return a.div(i.toFloat()).neg()}}})},e.divStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in divideStrict: "),e.div(t)},e.mod=function(e,t){assertArgumentsAreTensors({a:e,b:t},"mod"),assertTypesMatch(e,t);var n=assertAndGetBroadcastShape(e.shape,t.shape);return ENV.engine.runKernel(function(n){return n.mod(e,t)},{a:e,b:t},function(r){return {a:function(){var t=getReductionAxes(e.shape,n);return t.length>0?r.sum(t).reshape(e.shape):r},b:function(){var a=r.mul(e.div(t).floor().neg()),o=getReductionAxes(t.shape,n);return o.length>0?a.sum(o).reshape(t.shape):a}}})},e.modStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in modStrict: "),e.mod(t)},e.minimum=function(e,t){assertArgumentsAreTensors({a:e,b:t},"minimum"),assertTypesMatch(e,t),"bool"===e.dtype&&(e=e.toInt()),"bool"===t.dtype&&(t=t.toInt()),assertAndGetBroadcastShape(e.shape,t.shape);return ENV.engine.runKernel(function(n){return n.minimum(e,t)},{a:e,b:t},function(n){return {a:function(){return n.mul(e.lessEqual(t).toFloat())},b:function(){return n.mul(e.greater(t).toFloat())}}})},e.minimumStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in minimumStrict: "),e.minimum(t)},e.maximum=function(e,t){assertArgumentsAreTensors({a:e,b:t},"maximum"),assertTypesMatch(e,t),"bool"===e.dtype&&(e=e.toInt()),"bool"===t.dtype&&(t=t.toInt()),assertAndGetBroadcastShape(e.shape,t.shape);return ENV.engine.runKernel(function(n){return n.maximum(e,t)},{a:e,b:t},function(n){return {a:function(){return n.mul(e.greaterEqual(t).toFloat())},b:function(){return n.mul(e.less(t).toFloat())}}})},e.maximumStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in minimumStrict: "),e.maximum(t)},e.squaredDifference=function(e,t){assertArgumentsAreTensors({a:e,b:t},"squaredDifference"),assertTypesMatch(e,t),assertAndGetBroadcastShape(e.shape,t.shape);return ENV.engine.runKernel(function(n){return n.squaredDifference(e,t)},{a:e,b:t},function(n){var r=scalar(2);return {a:function(){return n.mul(e.sub(t).mul(r))},b:function(){return n.mul(t.sub(e).mul(r))}}})},e.squaredDifferenceStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in squaredDifferenceStrict: "),e.squaredDifference(t)},e.atan2=function(t,n){assertArgumentsAreTensors({a:t,b:n},"atan2"),assertTypesMatch(t,n);var r=assertAndGetBroadcastShape(t.shape,n.shape);return ENV.engine.runKernel(function(e){return e.atan2(t,n)},{a:t,b:n},function(a){return {a:function(){var o=e.add(square(t),square(n)),i=a.mul(n.div(o)),s=getReductionAxes(t.shape,r);return s.length>0&&(i=i.sum(s)),i.reshape(t.shape)},b:function(){var o=e.add(square(t),square(n)),i=neg(a.mul(t.div(o))),s=getReductionAxes(n.shape,r);return s.length>0&&(i=i.sum(s)),i.reshape(n.shape)}}})},__decorate([doc({heading:"Operations",subheading:"Arithmetic"}),operation],e,"add",null),__decorate([operation],e,"addStrict",null),__decorate([doc({heading:"Operations",subheading:"Arithmetic"}),operation],e,"sub",null),__decorate([operation],e,"subStrict",null),__decorate([doc({heading:"Operations",subheading:"Arithmetic"}),operation],e,"pow",null),__decorate([operation],e,"powStrict",null),__decorate([doc({heading:"Operations",subheading:"Arithmetic"}),operation],e,"mul",null),__decorate([operation],e,"mulStrict",null),__decorate([doc({heading:"Operations",subheading:"Arithmetic"}),operation],e,"div",null),__decorate([doc({heading:"Operations",subheading:"Arithmetic"}),operation],e,"floorDiv",null),__decorate([operation],e,"divStrict",null),__decorate([doc({heading:"Operations",subheading:"Arithmetic"}),operation],e,"mod",null),__decorate([operation],e,"modStrict",null),__decorate([doc({heading:"Operations",subheading:"Arithmetic"}),operation],e,"minimum",null),__decorate([operation],e,"minimumStrict",null),__decorate([doc({heading:"Operations",subheading:"Arithmetic"}),operation],e,"maximum",null),__decorate([operation],e,"maximumStrict",null),__decorate([doc({heading:"Operations",subheading:"Arithmetic"}),operation],e,"squaredDifference",null),__decorate([operation],e,"squaredDifferenceStrict",null),__decorate([doc({heading:"Operations",subheading:"Basic math"}),operation],e,"atan2",null),e}(),CompareOps=function(){function e(){}return e.notEqual=function(e,t){return assertArgumentsAreTensors({a:e,b:t},"notEqual"),assertTypesMatch(e,t),assertAndGetBroadcastShape(e.shape,t.shape),ENV.engine.runKernel(function(n){return n.notEqual(e,t)},{a:e,b:t})},e.notEqualStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in notEqualStrict: "),e.notEqual(t)},e.less=function(e,t){return assertArgumentsAreTensors({a:e,b:t},"less"),assertTypesMatch(e,t),assertAndGetBroadcastShape(e.shape,t.shape),ENV.engine.runKernel(function(n){return n.less(e,t)},{a:e,b:t})},e.lessStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in lessStrict: "),e.less(t)},e.equal=function(e,t){return assertArgumentsAreTensors({a:e,b:t},"equal"),assertTypesMatch(e,t),assertAndGetBroadcastShape(e.shape,t.shape),ENV.engine.runKernel(function(n){return n.equal(e,t)},{a:e,b:t})},e.equalStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in equalStrict: "),e.equal(t)},e.lessEqual=function(e,t){return assertArgumentsAreTensors({a:e,b:t},"lessEqual"),assertTypesMatch(e,t),assertAndGetBroadcastShape(e.shape,t.shape),ENV.engine.runKernel(function(n){return n.lessEqual(e,t)},{a:e,b:t})},e.lessEqualStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in lessEqualStrict: "),e.lessEqual(t)},e.greater=function(e,t){return assertArgumentsAreTensors({a:e,b:t},"greater"),assertTypesMatch(e,t),assertAndGetBroadcastShape(e.shape,t.shape),ENV.engine.runKernel(function(n){return n.greater(e,t)},{a:e,b:t})},e.greaterStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in greaterStrict: "),e.greater(t)},e.greaterEqual=function(e,t){return assertArgumentsAreTensors({a:e,b:t},"greaterEqual"),assertTypesMatch(e,t),assertAndGetBroadcastShape(e.shape,t.shape),ENV.engine.runKernel(function(n){return n.greaterEqual(e,t)},{a:e,b:t})},e.greaterEqualStrict=function(e,t){return assertShapesMatch(e.shape,t.shape,"Error in greaterEqualStrict: "),e.greaterEqual(t)},__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"notEqual",null),__decorate([operation],e,"notEqualStrict",null),__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"less",null),__decorate([operation],e,"lessStrict",null),__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"equal",null),__decorate([operation],e,"equalStrict",null),__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"lessEqual",null),__decorate([operation],e,"lessEqualStrict",null),__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"greater",null),__decorate([operation],e,"greaterStrict",null),__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"greaterEqual",null),__decorate([operation],e,"greaterEqualStrict",null),e}(),LogicalOps=function(){function e(){}return e.logicalNot=function(e){return assertArgumentsAreTensors({x:e},"logicalNot"),assert("bool"===e.dtype,"Error Array must be of type bool."),ENV.engine.runKernel(function(t){return t.logicalNot(e)},{x:e})},e.logicalAnd=function(e,t){return assertArgumentsAreTensors({a:e,b:t},"logicalAnd"),assert("bool"===e.dtype&&"bool"===t.dtype,"Error Array must be of type bool."),assertAndGetBroadcastShape(e.shape,t.shape),ENV.engine.runKernel(function(n){return n.logicalAnd(e,t)},{a:e,b:t})},e.logicalOr=function(e,t){return assertArgumentsAreTensors({a:e,b:t},"logicalOr"),assert("bool"===e.dtype&&"bool"===t.dtype,"Error Array must be of type bool."),assertAndGetBroadcastShape(e.shape,t.shape),ENV.engine.runKernel(function(n){return n.logicalOr(e,t)},{a:e,b:t})},e.logicalXor=function(t,n){return assertArgumentsAreTensors({a:t,b:n},"logicalXor"),assert("bool"===t.dtype&&"bool"===n.dtype,"Error Array must be of type bool."),assertAndGetBroadcastShape(t.shape,n.shape),e.logicalOr(t,n).logicalAnd(e.logicalAnd(t,n).logicalNot())},e.where=function(e,t,n){assertArgumentsAreTensors({condition:e,a:t,b:n},"where"),assert("bool"===e.dtype,"Error Condition must be of type bool."),assertShapesMatch(t.shape,n.shape,"Error in where: "),1===e.rank?assert(e.shape[0]===t.shape[0],"The first dimension of `a` must match the size of `condition`."):assertShapesMatch(e.shape,n.shape,"Error in where: ");var r=upcastType(t.dtype,n.dtype);return ENV.engine.runKernel(function(a){return a.where(e,t,n,r)},{condition:e,a:t,b:n},function(r){return {condition:function(){return zerosLike(e)},a:function(){return r.mul(e.cast(t.dtype))},b:function(){return r.mul(e.logicalNot().cast(n.dtype))}}})},__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"logicalNot",null),__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"logicalAnd",null),__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"logicalOr",null),__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"logicalXor",null),__decorate([doc({heading:"Operations",subheading:"Logical"}),operation],e,"where",null),e}(),SegmentOps=function(){function e(){}return e.unsortedSegmentSum=function(e,t,n){assertArgumentsAreTensors({x:e,segmentIds:t},"unsortedSegmentSum"),assert("int32"===t.dtype,"segmentIds must be of dtype `int32`"),assert(isInt(n),"numSegments must be of dtype int");return ENV.engine.runKernel(function(r){return r.unsortedSegmentSum(e,t,n)},{x:e},function(e){return {x:function(){return gatherDropNegatives(e,t)}}})},__decorate([doc({heading:"Operations",subheading:"Segment"}),operation],e,"unsortedSegmentSum",null),e}();function gatherDropNegatives(e,t){for(var n=BinaryOps.maximum(t,ArrayOps.zerosLike(t)),r=ArrayOps.gather(e,n),a=CompareOps.greaterEqual(t,ArrayOps.scalar(0,"int32")),o=r.rank-a.rank,i=0;i2)throw new Error("Rank of probabilities must be 1 or 2, but is "+o);n=n||Math.random();var i=1===o?e.as2D(1,-1):e,s=ENV.engine.runKernel(function(e){return e.multinomial(i,r,t,n)},{logits2D:i});return 1===o?s.as1D():s},e.oneHot=function(e,t,n,r){if(void 0===n&&(n=1),void 0===r&&(r=0),assert("int32"===e.dtype,"Indices must be of dtype `int32`"),t<2)throw new Error("Error in oneHot: depth must be >=2, but it is "+t);return ENV.engine.runKernel(function(a){return a.oneHot(e,t,n,r)},{indices:e})},e.fromPixels=function(e,t){if(void 0===t&&(t=3),t>4)throw new Error("Cannot construct Tensor with more than 4 channels from pixels.");return ENV.engine.fromPixels(e,t)},e.toPixels=function(e,t){return __awaiter(this,void 0,void 0,function(){var n,r,a,o,i,s,u,l,c,d,p,h,f,g,m,v,y,x,b;return __generator(this,function(E){switch(E.label){case 0:if(assertArgumentsAreTensors({img:e},"toPixels"),2!==e.rank&&3!==e.rank)throw new Error("toPixels only supports rank 2 or 3 tensors, got rank "+e.rank+".");if(n=e.shape.slice(0,2),r=n[0],a=n[1],(o=2===e.rank?1:e.shape[2])>4||2===o)throw new Error("toPixels only supports depth of size 1, 3 or 4 but got "+o);return i=e.min(),s=e.max(),[4,i.data()];case 1:return u=E.sent()[0],[4,s.data()];case 2:if(l=E.sent()[0],i.dispose(),s.dispose(),"float32"===e.dtype){if(u<0||l>1)throw new Error("Tensor values for a float32 Tensor must be in the range [0 - 1] but got range ["+u+" - "+l+"].")}else{if("int32"!==e.dtype)throw new Error("Unsupported type for toPixels: "+e.dtype+". Please use float32 or int32 tensors.");if(u<0||l>255)throw new Error("Tensor values for a int32 Tensor must be in the range [0 - 255] but got range ["+u+" - "+l+"].")}return [4,e.data()];case 3:for(c=E.sent(),d="float32"===e.dtype?255:1,p=new Uint8ClampedArray(a*r*4),h=0;h=1,"Pass at least one tensor to tf.stack"),1===e.length)return e[0].expandDims(t);var n=e[0].rank,r=e[0].shape,a=e[0].dtype;assert(t<=n,"Axis must be <= rank of the tensor"),e.forEach(function(e){assertShapesMatch(r,e.shape,"All tensors passed to stack must have matching shapes");}),e.forEach(function(e){assert(a===e.dtype,"All tensors passed to stack must have matching dtypes");});var o=e.map(function(e){return e.expandDims(t)});return ConcatOps.concat(o,t)},e.unstack=function(e,t){void 0===t&&(t=0);for(var n,r=e.shape[t],a=Array(e.rank-1).fill(0),o=0,i=0;i1)return e.zeros([0],a);var o=makeZerosTypedArray(Math.abs(Math.ceil((n-t)/r)),a);n0,"Gram-Schmidt process: input must not be null, undefined, or empty");for(var n=e[0].shape[0],r=1;r0)for(var n=0;n0?a>=s[t]:a<=s[t]);a+=r[t])n+=1;return n}),[i,l]}function startForAxis(e,t,n,r,a){var o=t[a];e&1<0?Number.MIN_SAFE_INTEGER:Number.MAX_SAFE_INTEGER);var i=r[a];return o<0&&(o+=i),o=clamp(0,o,i-1)}function stopForAxis(e,t,n,r,a){var o=t[a];e&1<0?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER);var i=r[a];return o<0&&(o+=i),o=n[a]>0?clamp(0,o,i):clamp(-1,o,i-1)}var SliceOps=function(){function e(){}return e.slice1d=function(t,n,r){return assert(1===t.rank,"slice1d expects a rank-1 tensor, but got a rank-"+t.rank+" tensor"),e.slice(t,[n],[r])},e.slice2d=function(t,n,r){return assert(2===t.rank,"slice1d expects a rank-2 tensor, but got a rank-"+t.rank+" tensor"),e.slice(t,n,r)},e.slice3d=function(t,n,r){return assert(3===t.rank,"slice1d expects a rank-3 tensor, but got a rank-"+t.rank+" tensor"),e.slice(t,n,r)},e.slice4d=function(t,n,r){return assert(4===t.rank,"slice1d expects a rank-4 tensor, but got a rank-"+t.rank+" tensor"),e.slice(t,n,r)},e.slice=function(e,t,n){if(assertArgumentsAreTensors({x:e},"slice"),0===e.rank)throw new Error("Slicing scalar is not possible");var r,a;r="number"==typeof t?[t].concat(new Array(e.rank-1).fill(0)):t.length=0?t:(assert(-1===t,"Bad value in size"),e.shape[n]-r[n])}),assertParamsValid(e,r,a);var o=e.shape;return ENV.engine.runKernel(function(t){return t.slice(e,r,a)},{x:e},function(e){for(var t=[],n=0;n=0&&n=0;--r)n[r]=n[r+1]*e[r+1];return n}var Gradients=function(){function e(){}return e.gradScope=function(e,t){return tidy(e,t,!0)},e.grad=function(e){return assert(isFunction(e),"The f passed in grad(f) must be a function"),function(t,n){return assert(t instanceof Tensor,"The x passed in grad(f)(x) must be a tensor"),assert(null==n||n instanceof Tensor,"The dy passed in grad(f)(x, dy) must be a tensor"),tidy(function(){var r=ENV.engine.gradients(function(){return e(t)},[t],n),a=r.value,o=r.grads;return null!=n&&assertShapesMatch(a.shape,n.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),checkGrads(o),o[0]})}},e.grads=function(e){return assert(isFunction(e),"The f passed in grads(f) must be a function"),function(t,n){return assert(Array.isArray(t)&&t.every(function(e){return e instanceof Tensor}),"The args passed in grads(f)(args) must be an array of tensors"),assert(null==n||n instanceof Tensor,"The dy passed in grads(f)(args, dy) must be a tensor"),tidy(function(){var r=ENV.engine.gradients(function(){return e.apply(void 0,t)},t,n),a=r.value,o=r.grads;return null!=n&&assertShapesMatch(a.shape,n.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),checkGrads(o),o})}},e.valueAndGrad=function(e){return assert(isFunction(e),"The f passed in valueAndGrad(f) must be a function"),function(t,n){assert(t instanceof Tensor,"The x passed in valueAndGrad(f)(x) must be a tensor"),assert(null==n||n instanceof Tensor,"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");var r=ENV.engine.gradients(function(){return e(t)},[t],n),a=r.grads,o=r.value;return checkGrads(a),{grad:a[0],value:o}}},e.valueAndGrads=function(e){return assert(isFunction(e),"The f passed in valueAndGrads(f) must be a function"),function(t,n){assert(Array.isArray(t)&&t.every(function(e){return e instanceof Tensor}),"The args passed in valueAndGrads(f)(args) must be array of tensors"),assert(null==n||n instanceof Tensor,"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");var r=ENV.engine.gradients(function(){return e.apply(void 0,t)},t,n);return null!=n&&assertShapesMatch(r.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),checkGrads(r.grads),r}},e.variableGrads=function(e,t){if(assert(isFunction(e),"The f passed in variableGrads(f) must be a function"),assert(null==t||Array.isArray(t)&&t.every(function(e){return e instanceof Variable}),"The varList passed in variableGrads(f, varList) must be an array of variables"),null==t)for(var n in t=[],ENV.engine.registeredVariables)t.push(ENV.engine.registeredVariables[n]);var r=t.length;assert((t=t.filter(function(e){return e.trainable})).length>0,"variableGrads() expects at least one of the input variables to be trainable, but none of the "+r+" variables is trainable.");var a=ENV.engine.gradients(e,t,null,!0),o=a.value,i=a.grads;assert(i.some(function(e){return null!=e}),"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()."),assert(0===o.rank,"The f passed in variableGrads(f) must return a scalar, but it returned a rank-"+o.rank+" tensor");var s={};return t.forEach(function(e,t){null!=i[t]&&(s[e.name]=i[t]);}),{value:o,grads:s}},e.customGrad=function(e){return ENV.engine.customGrad(e)},__decorate([doc({heading:"Training",subheading:"Gradients"})],e,"grad",null),__decorate([doc({heading:"Training",subheading:"Gradients"})],e,"grads",null),__decorate([doc({heading:"Training",subheading:"Gradients"})],e,"valueAndGrad",null),__decorate([doc({heading:"Training",subheading:"Gradients"})],e,"valueAndGrads",null),__decorate([doc({heading:"Training",subheading:"Gradients"})],e,"variableGrads",null),__decorate([doc({heading:"Training",subheading:"Gradients"})],e,"customGrad",null),e}();function checkGrads(e){if(e.filter(function(e){return null==e}).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 tidy=Tracking.tidy,keep=Tracking.keep,dispose=Tracking.dispose,time=Tracking.time,grad=Gradients.grad,valueAndGrad=Gradients.valueAndGrad,grads=Gradients.grads,valueAndGrads=Gradients.valueAndGrads,variableGrads=Gradients.variableGrads,customGrad=Gradients.customGrad,Profiler=function(){function e(e,t){this.backendTimer=e,this.logger=t,null==t&&(this.logger=new Logger);}return e.prototype.profileKernel=function(e,t){var n,r=this,a=this.backendTimer.time(function(){n=t();}),o=n.dataSync();return checkForNaN(o,n.dtype,e),a.then(function(t){r.logger.logKernelProfile(e,n,o,t.kernelMs);}),n},e}(),Logger=function(){function e(){}return e.prototype.logKernelProfile=function(e,t,n,r){var a=rightPad(r+"ms",9),o=rightPad(e,25),i=t.rank,s=t.size,u=rightPad(t.shape.toString(),14);console.log("%c"+o+"\t%c"+a+"\t%c"+i+"D "+u+"\t%c"+s,"font-weight:bold","color:red","color:blue","color: orange");},e}();function getFilteredNodesXToY(e,t,n){for(var r={},a={},o=0;o=0;o--){i=(g=e[o]).inputs;var h=[];h.push(g.output);for(c=0;c=0;n--){var r=t[n],a=e[r.output.id];if(null==r.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+r.name+".");var o=r.gradient(a);for(var i in r.inputs){if(!(i in o))throw new Error("Cannot backprop through input "+i+". Available gradients found: "+Object.keys(o)+".");var s=o[i](),u=r.inputs[i];if(!arraysEqual(s.shape,u.shape))throw new Error("Error in gradient for op "+r.name+". The gradient of input '"+i+"' has shape '"+s.shape+"', which does not match the shape of the input '"+u.shape+"'");if(null==e[u.id])e[u.id]=s;else{var l=e[u.id];e[u.id]=l.add(s),l.dispose();}}}}var Type,Engine=function(){function e(e,t){this.backend=e,this.safeMode=t,this.registeredVariables={},this.refCounter=new WeakMap,this.nextTapeNodeId=0,this.numBytes=0,this.numTensors=0,this.numDataBuffers=0,this.gradientScopeCount=0,this.customGradientDepth=0,this.keepTensors=new Set,this.activeScope={track:[]},this.scopeStack=[this.activeScope],this.profiler=new Profiler(e);}return e.prototype.runKernel=function(e,t,n){var r,a=this,o=[],i=function(e){return o.push(e),e},s=this.activeScope.name;if(this.customGradientDepth++,r=ENV.get("DEBUG")?this.profiler.profileKernel(s,function(){return e(a.backend,i)}):e(this.backend,i),this.customGradientDepth--,this.shouldRecord()){var u={id:this.nextTapeNodeId++,name:s,inputs:t,output:r};null!=n&&(u.gradient=function(e){return n(e,o)}),this.activeTape.push(u);}return r},e.prototype.registerTensor=function(e){var t=this.refCounter.has(e.dataId)?this.refCounter.get(e.dataId):0;this.numTensors++,0===t&&(this.numDataBuffers++,this.numBytes+=sizeFromShape(e.shape)*bytesPerElement(e.dtype),this.backend.register(e.dataId,e.shape,e.dtype)),this.refCounter.set(e.dataId,t+1),e instanceof Variable||this.track(e);},e.prototype.registerVariable=function(e){if(null!=this.registeredVariables[e.name])throw new Error("Variable with name "+e.name+" was already registered");this.registeredVariables[e.name]=e;},e.prototype.disposeTensor=function(e){if(this.refCounter.has(e.dataId)){this.keepTensors.has(e.id)&&this.keepTensors.delete(e.id),this.numTensors--;var t=this.refCounter.get(e.dataId);t<=1?(this.refCounter.delete(e.dataId),this.backend.disposeData(e.dataId),this.numDataBuffers--,this.numBytes-=sizeFromShape(e.shape)*bytesPerElement(e.dtype)):this.refCounter.set(e.dataId,t-1);}},e.prototype.disposeVariables=function(){for(var e in this.registeredVariables){var t=this.registeredVariables[e];this.disposeTensor(t),delete this.registeredVariables[e];}},e.prototype.memory=function(){var e=this.backend.memory();return e.numTensors=this.numTensors,e.numDataBuffers=this.numDataBuffers,e.numBytes=this.numBytes,e},e.prototype.shouldRecord=function(){return null!=this.activeTape&&0===this.customGradientDepth},e.prototype.addTapeNode=function(e,t,n){var r={};e.forEach(function(e,t){r[t]=e;});var a={id:this.nextTapeNodeId++,name:this.activeScope.name,inputs:r,output:t,gradient:function(e){var t={};return n(e).forEach(function(e,n){t[n]=function(){return e};}),t}};this.activeTape.push(a);},e.prototype.keep=function(e){if(1===this.scopeStack.length&&ENV.engine.safeMode)throw new Error("Safe mode is ON. Enclose all tensor operations inside tf.tidy(): tf.tidy(() => {...}) to avoid memory leaks.");return this.keepTensors.add(e.id),e},e.prototype.startScope=function(e,t){void 0===t&&(t=!1),t&&0===this.gradientScopeCount&&(this.activeTape=[]),t&&this.gradientScopeCount++;var n={track:[]};e&&(n.name=e),this.scopeStack.push(n),this.activeScope=n;},e.prototype.endScope=function(e,t){var n=this;void 0===t&&(t=!1),t&&(this.gradientScopeCount--,0===this.gradientScopeCount&&(this.activeTape=null));var r=new Set(this.keepTensors),a=getTensorsInContainer(e);a.forEach(function(e){return r.add(e.id)});for(var o=0;o0,"gradients() received an empty list of xs."),tidy("gradients",function(){var o=e();assert(o instanceof Tensor,"The result y returned by f() must be a tensor.");var i=getFilteredNodesXToY(a.activeTape,t,o);if(!r&&0===i.length&&t.length>0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that the f you passed encloses all operations that lead from x to y.");var s={};return s[o.id]=null==n?ones(o.shape):n,backpropagateGradients(s,i),{value:o,grads:t.map(function(e){return s[e.id]})}},!0)},e.prototype.customGrad=function(e){var t=this;return assert(isFunction(e),"The f passed in customGrad(f) must be a function."),function(){for(var n,r=[],a=0;a {op();...}); to avoid memory leaks.");return this.activeScope.track.push(e),e},e}();!function(e){e[e.NUMBER=0]="NUMBER",e[e.BOOLEAN=1]="BOOLEAN",e[e.STRING=2]="STRING";}(Type||(Type={}));var URL_PROPERTIES=[{name:"DEBUG",type:Type.BOOLEAN},{name:"IS_BROWSER",type:Type.BOOLEAN},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",type:Type.NUMBER},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",type:Type.BOOLEAN},{name:"WEBGL_VERSION",type:Type.NUMBER},{name:"WEBGL_RENDER_FLOAT32_ENABLED",type:Type.BOOLEAN},{name:"WEBGL_DOWNLOAD_FLOAT_ENABLED",type:Type.BOOLEAN},{name:"WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED",type:Type.BOOLEAN},{name:"BACKEND",type:Type.STRING}],TEST_EPSILON_FLOAT32_ENABLED=.001,TEST_EPSILON_FLOAT32_DISABLED=.1;function hasExtension(e,t){return null!=e.getExtension(t)}function getWebGLRenderingContext(e){if(0===e||!ENV.get("IS_BROWSER"))throw new Error("Cannot get WebGL rendering context, WebGL is disabled.");var t=document.createElement("canvas");return 1===e?t.getContext("webgl")||t.getContext("experimental-webgl"):t.getContext("webgl2")}function loseContext(e){if(null!=e){var t=e.getExtension("WEBGL_lose_context");if(null==t)throw new Error("Extension WEBGL_lose_context not supported on this browser.");t.loseContext();}}function isWebGLVersionEnabled(e){var t;try{t=getWebGLRenderingContext(e);}catch(e){return !1}return null!=t&&(loseContext(t),!0)}function getWebGLDisjointQueryTimerVersion(e){if(0===e)return 0;var t,n=getWebGLRenderingContext(e);return t=hasExtension(n,"EXT_disjoint_timer_query_webgl2")&&2===e?2:hasExtension(n,"EXT_disjoint_timer_query")?1:0,null!=n&&loseContext(n),t}function createFloatTextureAndBindToFramebuffer(e,t){var n=e.createFramebuffer(),r=e.createTexture();e.bindTexture(e.TEXTURE_2D,r);var a=2===t?e.RGBA32F:e.RGBA;e.texImage2D(e.TEXTURE_2D,0,a,1,1,0,e.RGBA,e.FLOAT,null),e.bindFramebuffer(e.FRAMEBUFFER,n),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,r,0);}function isRenderToFloatTextureEnabled(e){if(0===e)return !1;var t=getWebGLRenderingContext(e);if(1===e){if(!hasExtension(t,"OES_texture_float"))return !1}else if(!hasExtension(t,"EXT_color_buffer_float"))return !1;createFloatTextureAndBindToFramebuffer(t,e);var n=t.checkFramebufferStatus(t.FRAMEBUFFER)===t.FRAMEBUFFER_COMPLETE;return loseContext(t),n}function isDownloadFloatTextureEnabled(e){if(0===e)return !1;var t=getWebGLRenderingContext(e);if(1===e){if(!hasExtension(t,"OES_texture_float"))return !1}else if(!hasExtension(t,"EXT_color_buffer_float"))return !1;createFloatTextureAndBindToFramebuffer(t,e),t.readPixels(0,0,1,1,t.RGBA,t.FLOAT,new Float32Array(4));var n=t.getError()===t.NO_ERROR;return loseContext(t),n}function isWebGLGetBufferSubDataAsyncExtensionEnabled(e){if(e>0)return !1;if(2!==e)return !1;var t=getWebGLRenderingContext(e),n=hasExtension(t,"WEBGL_get_buffer_sub_data_async");return loseContext(t),n}function isChrome(){return null!=navigator&&null!=navigator.userAgent&&/Chrome/.test(navigator.userAgent)&&/Google Inc/.test(navigator.vendor)}var Environment=function(){function e(e){this.features={},this.registry={},null!=e&&(this.features=e),this.get("DEBUG")&&console.warn("Debugging mode is ON. The output of every math call will be downloaded to CPU and checked for NaNs. This significantly impacts performance.");}return e.setBackend=function(e,t){if(void 0===t&&(t=!1),!(e in ENV.registry))throw new Error("Backend type '"+e+"' not found in registry");ENV.initBackend(e,t);},e.getBackend=function(){return ENV.initDefaultBackend(),ENV.currentBackend},e.disposeVariables=function(){ENV.engine.disposeVariables();},e.memory=function(){return ENV.engine.memory()},e.prototype.get=function(e){return e in this.features?this.features[e]:(this.features[e]=this.evaluateFeature(e),this.features[e])},e.prototype.getFeatures=function(){return this.features},e.prototype.set=function(e,t){this.features[e]=t;},e.prototype.getBestBackendType=function(){var e=this;if(0===Object.keys(this.registry).length)throw new Error("No backend found in registry.");return Object.keys(this.registry).map(function(t){return {name:t,entry:e.registry[t]}}).sort(function(e,t){return t.entry.priority-e.entry.priority})[0].name},e.prototype.evaluateFeature=function(e){if("DEBUG"===e)return !1;if("IS_BROWSER"===e)return "undefined"!=typeof window;if("IS_NODE"===e)return "undefined"!=typeof process&&void 0!==process.versions.node;if("IS_CHROME"===e)return isChrome();if("BACKEND"===e)return this.getBestBackendType();if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"===e){var t=this.get("WEBGL_VERSION");return 0===t?0:getWebGLDisjointQueryTimerVersion(t)}if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE"===e)return this.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0&&!isMobile();if("WEBGL_VERSION"===e)return isWebGLVersionEnabled(2)?2:isWebGLVersionEnabled(1)?1:0;if("WEBGL_RENDER_FLOAT32_ENABLED"===e)return isRenderToFloatTextureEnabled(this.get("WEBGL_VERSION"));if("WEBGL_DOWNLOAD_FLOAT_ENABLED"===e)return isDownloadFloatTextureEnabled(this.get("WEBGL_VERSION"));if("WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED"===e)return isWebGLGetBufferSubDataAsyncExtensionEnabled(this.get("WEBGL_VERSION"));if("TEST_EPSILON"===e)return this.get("WEBGL_RENDER_FLOAT32_ENABLED")?TEST_EPSILON_FLOAT32_ENABLED:TEST_EPSILON_FLOAT32_DISABLED;throw new Error("Unknown feature "+e+".")},e.prototype.setFeatures=function(e){this.features=e;},e.prototype.reset=function(){this.features=getFeaturesFromURL(),null!=this.globalEngine&&(this.globalEngine=null);},e.prototype.initBackend=function(e,t){void 0===t&&(t=!1),this.currentBackend=e;var n=ENV.findBackend(e);this.globalEngine=new Engine(n,t);},e.prototype.findBackend=function(e){return e in this.registry?this.registry[e].backend:null},e.prototype.registerBackend=function(e,t,n){void 0===n&&(n=1),e in this.registry&&console.warn(e+" backend was already registered");try{var r=t();return this.registry[e]={backend:r,priority:n},!0}catch(t){return console.warn("Registration of backend "+e+" failed"),console.warn(t.stack||t.message),!1}},e.prototype.removeBackend=function(e){if(!(e in this.registry))throw new Error(e+" backend not found in registry");this.registry[e].backend.dispose(),delete this.registry[e];},Object.defineProperty(e.prototype,"engine",{get:function(){return this.initDefaultBackend(),this.globalEngine},enumerable:!0,configurable:!0}),e.prototype.initDefaultBackend=function(){null==this.globalEngine&&this.initBackend(ENV.get("BACKEND"),!1);},__decorate([doc({heading:"Environment"})],e,"setBackend",null),__decorate([doc({heading:"Environment"})],e,"getBackend",null),__decorate([doc({heading:"Environment"})],e,"disposeVariables",null),__decorate([doc({heading:"Performance",subheading:"Memory"})],e,"memory",null),e}(),TENSORFLOWJS_FLAGS_PREFIX="tfjsflags";function getFeaturesFromURL(){var e={};if("undefined"==typeof window||void 0===window.location)return e;var t=getQueryParams(window.location.search);if(TENSORFLOWJS_FLAGS_PREFIX in t){var n={};t[TENSORFLOWJS_FLAGS_PREFIX].split(",").forEach(function(e){var t=e.split(":"),r=t[0],a=t[1];n[r]=a;}),URL_PROPERTIES.forEach(function(t){t.name in n&&(console.log("Setting feature override from URL "+t.name+": "+n[t.name]),t.type===Type.NUMBER?e[t.name]=+n[t.name]:t.type===Type.BOOLEAN?e[t.name]="true"===n[t.name]:t.type===Type.STRING?e[t.name]=n[t.name]:console.warn("Unknown URL param: "+t.name+"."));});}return e}function getGlobalNamespace(){var e;if("undefined"!=typeof window)e=window;else{if("undefined"==typeof global)throw new Error("Could not find a global object");e=global;}return e}function getOrMakeEnvironment(){var e=getGlobalNamespace();return e.ENV=e.ENV||new Environment(getFeaturesFromURL()),e.ENV}var ENV=getOrMakeEnvironment(),environment=Object.freeze({get Type(){return Type},URL_PROPERTIES:URL_PROPERTIES,Environment:Environment,ENV:ENV}),PARALLELIZE_THRESHOLD=30;function computeOptimalWindowSize(e){return e<=PARALLELIZE_THRESHOLD?e:nearestDivisor(e,Math.floor(Math.sqrt(e)))}function segOpComputeOptimalWindowSize(e,t){var n,r=!1;for(e<=PARALLELIZE_THRESHOLD?(n=e,r=!0):n=nearestDivisor(e,Math.floor(Math.sqrt(e)));!r;){if(n>t||n===e){r=!0;break}n=nearestDivisor(e,n+1);}return n}function computeOutShape$1(e,t,n){for(var r=[],a=e.length,o=0;o":"<",u=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * "+r+";\n\n int bestIndex = 0;\n float bestValue = getA(batch, inOffset);\n\n for (int i = 0; i < "+r+"; i++) {\n int inIdx = "+u+";\n float candidate = getA(batch, inIdx);\n if (candidate "+s+" bestValue) {\n bestValue = candidate;\n bestIndex = inIdx;\n }\n }\n setOutput(float(bestIndex));\n }\n ";}}(),AvgPool2DBackpropProgram=function(){return function(e){this.variableNames=["dy"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,o=t-1-e.padInfo.top,i=n-1-e.padInfo.left,s=1/(t*n);this.userCode="\n const ivec2 pads = ivec2("+o+", "+i+");\n const float avgMultiplier = float("+s+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n\n ivec2 dyRCCorner = coords.yz - pads;\n int dyRCorner = dyRCCorner.x;\n int dyCCorner = dyRCCorner.y;\n\n // Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.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 ";}}(),BatchNormProgram=function(){return function(e,t,n,r,a,o){this.outputShape=[],this.supportsBroadcasting=!0,this.variableNames=["x","mean","variance"],assertAndGetBroadcastShape(e,t),assertAndGetBroadcastShape(e,n);var i="0.0";null!=r&&(assertAndGetBroadcastShape(e,r),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");var s="1.0";null!=a&&(assertAndGetBroadcastShape(e,a),this.variableNames.push("scale"),s="getScaleAtOutCoords()"),this.outputShape=e,this.userCode="\n void main() {\n float x = getXAtOutCoords();\n float mean = getMeanAtOutCoords();\n float variance = getVarianceAtOutCoords();\n float offset = "+i+";\n float scale = "+s+";\n float inv = scale * inversesqrt(variance + float("+o+"));\n setOutput((x - mean) * inv + offset);\n }\n ";}}(),CHECK_NAN_SNIPPET="\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n",ADD="return a + b;",SUB="return a - b;",MUL="return a * b;",DIV="return a / b;",INT_DIV="\n float resultSign = sign(a) * sign(b);\n int ia = round(a);\n int ib = round(b);\n int result = ia / ib;\n int amodb = ia - ib * result;\n\n if (resultSign < 0.0 && amodb != 0) {\n result -= 1;\n }\n return float(result);\n",POW="\n return (round(mod(b, 2.0)) == 0 || round(mod(b, 2.0)) == 2) ?\n pow(abs(a), b) : sign(a) * pow(abs(a), b);\n",SQUARED_DIFFERENCE="return (a - b) * (a - b);",EQUAL="return float(a == b);",NOT_EQUAL="return float(a != b);",LESS="return float(a < b);",LESS_EQUAL="return float(a <= b);",GREATER="return float(a > b);",GREATER_EQUAL="return float(a >= b);",LOGICAL_AND="return float(a >= 1.0 && b >= 1.0);",LOGICAL_OR="return float(a >= 1.0 || b >= 1.0);",MAX=CHECK_NAN_SNIPPET+"\n return max(a, b);\n",MIN=CHECK_NAN_SNIPPET+"\n return min(a, b);\n",MOD="return mod(a, b);",ATAN2=CHECK_NAN_SNIPPET+"\n return atan(a, b);\n",ELU_DER="return (b >= 1.0) ? a : a * (b + 1.0);",BinaryOpProgram=function(){return function(e,t,n){this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.outputShape=assertAndGetBroadcastShape(t,n),this.userCode="\n float binaryOperation(float a, float b) {\n "+e+"\n }\n\n void main() {\n float a = getAAtOutCoords();\n float b = getBAtOutCoords();\n setOutput(binaryOperation(a, b));\n }\n ";}}(),ClipProgram=function(){return function(e,t,n){this.variableNames=["A"],this.outputShape=e;var r=t.toFixed(20),a=n.toFixed(20);this.userCode="\n void main() {\n float value = getAAtOutCoords();\n if (isNaN(value)) {\n setOutput(value);\n return;\n }\n\n setOutput(clamp(value, "+r+", "+a+"));\n }\n ";}}(),ConcatProgram=function(){return function(e,t){this.variableNames=["A","B"],this.outputShape=[],this.outputShape=computeOutShape(e,t,1),this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int yR = coords.x;\n int yC = coords.y;\n\n float value = 0.0;\n if (yC < "+e[1]+") {\n value = getA(yR, yC);\n } else {\n yC -= "+e[1]+";\n value = getB(yR, yC);\n }\n\n setOutput(value);\n }\n ";}}(),Conv2DDerFilterProgram=function(){return function(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;var t=e.strideHeight,n=e.strideWidth,r=e.padInfo.top,a=e.padInfo.left;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 < "+e.batchSize+"; b++) {\n for (int yR = 0; yR < "+e.outHeight+"; yR++) {\n int xR = wR + yR * "+t+" - "+r+";\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+e.outWidth+"; yC++) {\n int xC = wC + yC * "+n+" - "+a+";\n\n if (xC < 0 || xC >= "+e.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 ";}}(),Conv2DDerInputProgram=function(){return function(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,o=t-1-e.padInfo.top,i=n-1-e.padInfo.left;this.userCode="\n const ivec2 pads = ivec2("+o+", "+i+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords[3];\n\n ivec2 dyCorner = coords.yz - 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 < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+t+" - 1 - wR;\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.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 < "+e.outChannels+"; d2++) {\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n ";}}(),DepthwiseConv2DDerFilterProgram=function(){return function(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;var t=e.strideHeight,n=e.strideWidth,r=e.padInfo.top,a=e.padInfo.left,o=e.outChannels/e.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 * "+o+" + dm;\n\n float dotProd = 0.0;\n\n // TODO: Vec4 over the batch size\n for (int b = 0; b < "+e.batchSize+"; b++) {\n for (int yR = 0; yR < "+e.outHeight+"; yR++) {\n int xR = wR + yR * "+t+" - "+r+";\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+e.outWidth+"; yC++) {\n int xC = wC + yC * "+n+" - "+a+";\n\n if (xC < 0 || xC >= "+e.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 ";}}(),DepthwiseConv2DDerInputProgram=function(){return function(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,o=t-1-e.padInfo.top,i=n-1-e.padInfo.left,s=e.outChannels/e.inChannels;this.userCode="\n const ivec2 pads = ivec2("+o+", "+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 < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+t+" - 1 - wR;\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+n+" - 1 - wC;\n\n // TODO: Vec4 over the channelMul\n for (int dm = 0; dm < "+s+"; dm++) {\n int d2 = d1 * "+s+" + dm;\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, dm);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n ";}}(),Conv2DProgram=function(){return function(e){this.variableNames=["x","W"],this.outputShape=e.outShape;var t=e.padInfo.top,n=e.padInfo.left,r=e.strideHeight,a=e.strideWidth,o=e.dilationHeight,i=e.dilationWidth,s=e.filterHeight,u=e.filterWidth,l=4*Math.floor(e.inChannels/4),c=e.inChannels%4;this.userCode="\n const ivec2 strides = ivec2("+r+", "+a+");\n const ivec2 pads = ivec2("+t+", "+n+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d2 = coords[3];\n\n ivec2 xRCCorner = coords.yz * 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 < "+s+"; wR++) {\n int xR = xRCorner + wR * "+o+";\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+u+"; wC++) {\n int xC = xCCorner + wC * "+i+";\n\n if (xC < 0 || xC >= "+e.inWidth+") {\n continue;\n }\n\n for (int d1 = 0; d1 < "+l+"; d1 += 4) {\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 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 dotProd += dot(xValues, wValues);\n }\n\n if ("+(1===c)+") {\n dotProd +=\n getX(batch, xR, xC, "+l+") *\n getW(wR, wC, "+l+", d2);\n } else if ("+(2===c)+") {\n vec2 xValues = vec2(\n getX(batch, xR, xC, "+l+"),\n getX(batch, xR, xC, "+l+" + 1)\n );\n vec2 wValues = vec2(\n getW(wR, wC, "+l+", d2),\n getW(wR, wC, "+l+" + 1, d2)\n );\n dotProd += dot(xValues, wValues);\n } else if ("+(3===c)+") {\n vec3 xValues = vec3(\n getX(batch, xR, xC, "+l+"),\n getX(batch, xR, xC, "+l+" + 1),\n getX(batch, xR, xC, "+l+" + 2)\n );\n vec3 wValues = vec3(\n getW(wR, wC, "+l+", d2),\n getW(wR, wC, "+l+" + 1, d2),\n getW(wR, wC, "+l+" + 2, d2)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n }\n setOutput(dotProd);\n }\n ";}}(),DepthwiseConv2DProgram=function(){return function(e){this.variableNames=["x","W"],this.outputShape=e.outShape;var t=e.inHeight,n=e.inWidth,r=e.padInfo.top,a=e.padInfo.left,o=e.strideHeight,i=e.strideWidth,s=e.dilationHeight,u=e.dilationWidth,l=e.filterHeight,c=e.filterWidth,d=e.outChannels/e.inChannels;this.userCode="\n const ivec2 strides = ivec2("+o+", "+i+");\n const ivec2 pads = ivec2("+r+", "+a+");\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 // TODO(dsmilkov): Flatten the two for loops and vec4 the operations.\n for (int wR = 0; wR < "+l+"; wR++) {\n int xR = xRCorner + wR * "+s+";\n\n if (xR < 0 || xR >= "+t+") {\n continue;\n }\n\n for (int wC = 0; wC < "+c+"; wC++) {\n int xC = xCCorner + wC * "+u+";\n\n if (xC < 0 || xC >= "+n+") {\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 setOutput(dotProd);\n }\n ";}}();function makeShader(e,t,n,r){var a=e.map(function(e){var t=sizeFromShape(e.shapeInfo.logicalShape);return e.shapeInfo.isUniform?"uniform float "+e.name+(t>1?"["+t+"]":"")+";":"uniform sampler2D "+e.name+";"});a=a.join("\n");var o=e.map(function(e){return getInputSamplingSnippet(e,t,r)}).join("\n"),i=t.texShape,s=getOutputSamplingSnippet(t.logicalShape,i);return [SHADER_PREFIX,FLOAT_TEXTURE_SAMPLE_SNIPPET,FLOAT_TEXTURE_SETOUTPUT_SNIPPET,a,s,o,n].join("\n")}function getSamplerFromInInfo(e){var t=e.shapeInfo.logicalShape;switch(t.length){case 0:return getSamplerScalar(e);case 1:return getSampler1D(e);case 2:return getSampler2D(e);case 3:return getSampler3D(e);case 4:return getSampler4D(e);case 5:return getSampler5D(e);case 6:return getSampler6D(e);default:throw new Error(t.length+"-D input sampling is not yet supported")}}function getInputSamplingSnippet(e,t,n){var r=getSamplerFlat(e);return r+=getSamplerFromInInfo(e),(n||arraysEqual(e.shapeInfo.logicalShape,t.logicalShape))&&(r+=getSamplerAtOutputCoords(e,t,n)),r}function getOutputSamplingSnippet(e,t){switch(e.length){case 0:return getOutputScalarCoords();case 1:return getOutput1DCoords(e,t);case 2:return getOutput2DCoords(e,t);case 3:return getOutput3DCoords(e,t);case 4:return getOutput4DCoords(e,t);case 5:return getOutput5DCoords(e,t);case 6:return getOutput6DCoords(e,t);default:throw new Error(e.length+"-D output sampling is not yet supported")}}var SAMPLE_1D_SNIPPET="\nvec2 UVfrom1D(int texNumR, int texNumC, int index) {\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n",SAMPLE_2D_SNIPPET="\nvec2 UVfrom2D(int texNumR, int texNumC, int numC, int row, int col) {\n int index = row * numC + col;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n",SAMPLE_3D_SNIPPET="\nvec2 UVfrom3D(int texNumR, int texNumC, int stride0,\n int stride1, int row, int col, int depth) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n",SAMPLE_4D_SNIPPET="\nvec2 UVfrom4D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int row, int col, int depth,\n int depth2) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth * stride2 + depth2;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n",SAMPLE_5D_SNIPPET="\nvec2 UVfrom5D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int stride3, int row, int col, int depth,\n int depth2, int depth3) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 +\n depth * stride2 + depth2 * stride3 + depth3;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n",SAMPLE_6D_SNIPPET="\nvec2 UVfrom6D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int stride3, int stride4,\n int row, int col, int depth, int depth2, int depth3, int depth4) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth * stride2 + depth2 *\n stride3 + depth3 * stride4 + depth4;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n",FLOAT_TEXTURE_SAMPLE_SNIPPET="\n float sampleTexture(sampler2D textureSampler, vec2 uv) {\n return texture2D(textureSampler, uv).r;\n }\n",FLOAT_TEXTURE_SETOUTPUT_SNIPPET="\n void setOutput(float val) {\n gl_FragColor = vec4(val, 0, 0, 0);\n }\n",SHADER_PREFIX="\n precision highp float;\n precision highp int;\n varying vec2 resultUV;\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 bool isNaN(float val) {\n return (val < 0.0 || 0.0 < val || val == 0.0) ? false : true;\n }\n\n bool hasNaN(vec4 values) {\n vec4 v1 = values * values;\n vec4 v2 = values * values;\n return any(notEqual(v1, v2));\n }\n\n float getNaN(vec4 values) {\n return dot(vec4(1), values);\n }\n\n int round(float value) {\n return int(floor(value + 0.5));\n }\n\n int imod(int x, int y) {\n return x - y * (x / y);\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 "+SAMPLE_1D_SNIPPET+"\n "+SAMPLE_2D_SNIPPET+"\n "+SAMPLE_3D_SNIPPET+"\n "+SAMPLE_4D_SNIPPET+"\n "+SAMPLE_5D_SNIPPET+"\n "+SAMPLE_6D_SNIPPET+"\n";function getOutputScalarCoords(){return "\n int getOutputCoords() {\n return 0;\n }\n "}function getOutput1DCoords(e,t){return 1===t[0]?"\n int getOutputCoords() {\n return int(resultUV.x * "+t[1]+".0);\n }\n ":1===t[1]?"\n int getOutputCoords() {\n return int(resultUV.y * "+t[0]+".0);\n }\n ":"\n int getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n return resTexRC.x * "+t[1]+" + resTexRC.y;\n }\n "}function getOutput3DCoords(e,t){var n=e[1]*e[2],r=e[2];return "\n ivec3 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n int r = index / "+n+";\n index -= r * "+n+";\n int c = index / "+r+";\n int d = index - c * "+r+";\n return ivec3(r, c, d);\n }\n "}function getOutput4DCoords(e,t){var n=e[3],r=e[2]*n,a=e[1]*r;return "\n ivec4 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n\n int r = index / "+a+";\n index -= r * "+a+";\n\n int c = index / "+r+";\n index -= c * "+r+";\n\n int d = index / "+n+";\n int d2 = index - d * "+n+";\n\n return ivec4(r, c, d, d2);\n }\n "}function getOutput5DCoords(e,t){var n=e[4],r=e[3]*n,a=e[2]*r,o=e[1]*a;return "\n ivec5 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx * vec2("+t[0]+",\n "+t[1]+"));\n\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n\n int r = index / "+o+";\n index -= r * "+o+";\n\n int c = index / "+a+";\n index -= c * "+a+";\n\n int d = index / "+r+";\n index -= d * "+r+";\n\n int d2 = index / "+n+";\n int d3 = index - d2 * "+n+";\n\n ivec5 outShape = ivec5(r, c, d, d2, d3);\n return outShape;\n }\n "}function getOutput6DCoords(e,t){var n=e[5],r=e[4]*n,a=e[3]*r,o=e[2]*a,i=e[1]*o;return "\n ivec6 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n\n int r = index / "+i+";\n index -= r * "+i+";\n\n int c = index / "+o+";\n index -= c * "+o+";\n\n int d = index / "+a+";\n index -= d * "+a+";\n\n int d2 = index / "+r+";\n index -= d2 * "+r+";\n\n int d3 = index / "+n+";\n int d4 = index - d3 * "+n+";\n\n ivec6 result = ivec6(r, c, d, d2, d3, d4);\n return result;\n }\n "}function getOutput2DCoords(e,t){return arraysEqual(e,t)?"\n ivec2 getOutputCoords() {\n return ivec2(resultUV.yx * vec2("+t[0]+", "+t[1]+"));\n }\n ":1===e[1]?"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n return ivec2(index, 0);\n }\n ":1===e[0]?"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n return ivec2(0, index);\n }\n ":"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n int r = index / "+e[1]+";\n int c = index - r * "+e[1]+";\n return ivec2(r, c);\n }\n "}function getSamplerScalar(e){var t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1);return e.shapeInfo.isUniform?"float "+n+"() {return "+t+";}":"\n float "+n+"() {\n return sampleTexture("+t+", halfCR);\n }\n "}function getSampler1D(e){var t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1);return "\n float "+n+"(int index) {\n return "+n+"Flat(index);\n }\n "}function getSampler2D(e){var t=e.shapeInfo.logicalShape,n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),a=e.shapeInfo.texShape;if(null!=a&&arraysEqual(t,a)){var o=a[0];return "\n float "+r+"(int row, int col) {\n vec2 uv = (vec2(col, row) + halfCR) / vec2("+a[1]+".0, "+o+".0);\n return sampleTexture("+n+", uv);\n }\n "}var i=squeezeShape(t),s=i.newShape,u=i.keptDims,l=s;if(l.length=1?"coords = 0;":s.map(function(e){return "coords["+(e+u)+"] = 0;"}).join("\n"))+"\n return get"+n+"("+(o<2&&a>0?"coords":e.shapeInfo.logicalShape.map(function(e,t){return "coords["+(t+u)+"]"}).join(", "))+");\n }\n "}function getSamplerAtOutputCoords(e,t,n){var r=e.name,a=r.charAt(0).toUpperCase()+r.slice(1),o="get"+a+"AtOutCoords",i=getBroadcastDims(e.shapeInfo.logicalShape,t.logicalShape),s=e.shapeInfo.logicalShape.length,u=t.logicalShape.length,l=n&&(u>s||i.length>0),c=broadcastDimsAreOuter(i),d=e.shapeInfo.isUniform;if(l&&!c)return getBroadcastOutputCoordsSampler(e,t,a,o);var p=sizeFromShape(e.shapeInfo.logicalShape),h="";l&&c&&(h="\n int mainPart = index / "+p+";\n index -= mainPart * "+p+";\n ");var f=t.texShape;if(d)return 1===p?"float "+o+"() {return "+r+";}":"\n float "+o+"() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+f[0]+", "+f[1]+"));\n int index = resTexRC.x * "+f[1]+" + resTexRC.y;\n "+h+"\n return get"+a+"Flat(index);\n }\n ";var g=e.shapeInfo.texShape;return arraysEqual(g,f)?"\n float "+o+"() {\n return sampleTexture("+r+", resultUV);\n }\n ":"\n float "+o+"() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+f[0]+", "+f[1]+"));\n int index = resTexRC.x * "+f[1]+" + resTexRC.y;\n "+h+"\n int texR = index / "+g[1]+";\n int texC = index - texR * "+g[1]+";\n vec2 uv = (vec2(texC, texR) + halfCR) /\n vec2("+g[1]+".0, "+g[0]+".0);\n\n return sampleTexture("+r+", uv);\n }\n "}function getCoordsDataType(e){if(e<=1)return "int";if(2===e)return "ivec2";if(3===e)return "ivec3";if(4===e)return "ivec4";if(5===e)return "ivec5";if(6===e)return "ivec6";throw Error("GPU for rank "+e+" is not yet supported")}function squeezeInputInfo(e,t){var n=JSON.parse(JSON.stringify(e));return n.shapeInfo.logicalShape=t,n}function getSqueezedParams(e,t){return t.map(function(t){return e[t]}).join(", ")}var CumSumProgram=function(){return function(e,t,n){this.variableNames=["x"],this.outputShape=e;var r=e.length,a=e[e.length-1],o=n?"<":">";this.userCode="\n int getIndex(int i) {\n "+(n?"return "+a+" -i - 1;":"return i;")+"\n }\n\n void main() {\n "+getCoordsDataType(r)+" coords = getOutputCoords();\n int end = "+getFinalCoord(r,"coords")+";\n float val = 0.0;\n for (int i = "+a+" - 1; i >= 0; i -= 1) {\n int idx = getIndex(i);\n if (idx "+o+" end) {\n continue;\n }\n if (idx == end && "+t+") {\n continue;\n }\n "+getFinalCoord(r,"coords")+" = idx;\n val += getX("+getCoords(r,"coords")+");\n }\n setOutput(val);\n }\n ";}}();function getCoords(e,t){if(1===e)return ""+t;if(2===e)return t+".x, "+t+".y";if(3===e)return t+".x, "+t+".y, "+t+".z";if(4===e)return t+".x, "+t+".y, "+t+".z, "+t+".w";throw Error("Cumulative sum for rank "+e+" is not yet supported")}function getFinalCoord(e,t){if(1===e)return ""+t;if(2===e)return t+".y";if(3===e)return t+".z";if(4===e)return t+".w";throw Error("Cumulative sum for rank "+e+" is not yet supported")}var TextureUsage,PhysicalTextureType,EncodeFloatProgram=function(){return function(e){this.variableNames=["A"],this.outputShape=e,this.userCode="\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\n void main() {\n float x = getAAtOutCoords();\n gl_FragColor = encode_float(x);\n }\n ";}}(),FromPixelsProgram=function(){return function(e){this.variableNames=["A"];var t=e[0],n=e[1];this.outputShape=e,this.userCode="\n void main() {\n ivec3 coords = getOutputCoords();\n int texR = coords[0];\n int texC = coords[1];\n int depth = coords[2];\n vec2 uv = (vec2(texC, texR) + halfCR) / vec2("+n+".0, "+t+".0);\n\n vec4 values = texture2D(A, uv);\n float value;\n if (depth == 0) {\n value = values.r;\n } else if (depth == 1) {\n value = values.g;\n } else if (depth == 2) {\n value = values.b;\n } else if (depth == 3) {\n value = values.a;\n }\n\n setOutput(floor(value * 255.0 + 0.5));\n }\n ";}}(),GatherProgram=function(){return function(e,t,n){this.variableNames=["A","indices"];var r=e.slice();r[n]=t,this.outputShape=r,this.rank=r.length;var a=getCoordsDataType(this.rank),o=getSourceCoords(e,n);this.userCode="\n void main() {\n "+a+" resRC = getOutputCoords();\n setOutput(getA("+o+"));\n }\n ";}}();function getSourceCoords(e,t){var n=e.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"],a=[],o=0;o= "+r);for(var a=0,o=0;o= "+r);for(var a=0,o=0;o= "+a);for(var o=getPackedMatrixTextureShapeWidthHeight(t,n),i=o[0],s=o[1],u=n%2==1,l=t%2==1,c=Math.floor(n/2),d=Math.floor(t/2),p=u?4:0,h=n,f=0,g=0;g= "+a);for(var o=n%2==1,i=t%2==1,s=Math.floor(n/2),u=Math.floor(t/2),l=getPackedMatrixTextureShapeWidthHeight(t,n),c=l[0],d=l[1],p=o?4:0,h=n+(o?1:0),f=0,g=0,m=n,v=0;vr||n>r){a="["+t+"x"+n+"]";throw new Error("Requested texture size "+a+" greater than WebGL maximum on this browser / GPU "+("["+r+"x"+r+"]")+".")}}function createFramebuffer(e){return throwIfNull(e,function(){return e.createFramebuffer()},"Unable to create WebGLFramebuffer.")}function bindVertexBufferToProgramAttribute(e,t,n,r,a,o,i){var s=e.getAttribLocation(t,n);return -1!==s&&(callAndCheck(e,function(){return e.bindBuffer(e.ARRAY_BUFFER,r)}),callAndCheck(e,function(){return e.vertexAttribPointer(s,a,e.FLOAT,!1,o,i)}),callAndCheck(e,function(){return e.enableVertexAttribArray(s)}),!0)}function bindTextureUnit(e,t,n){validateTextureUnit(e,n),callAndCheck(e,function(){return e.activeTexture(e.TEXTURE0+n)}),callAndCheck(e,function(){return e.bindTexture(e.TEXTURE_2D,t)});}function unbindTextureUnit(e,t){validateTextureUnit(e,t),callAndCheck(e,function(){return e.activeTexture(e.TEXTURE0+t)}),callAndCheck(e,function(){return e.bindTexture(e.TEXTURE_2D,null)});}function getProgramUniformLocationOrThrow(e,t,n){return throwIfNull(e,function(){return e.getUniformLocation(t,n)},'uniform "'+n+'" not present in program.')}function getProgramUniformLocation(e,t,n){return e.getUniformLocation(t,n)}function bindTextureToProgramUniformSampler(e,t,n,r,a){callAndCheck(e,function(){return bindTextureUnit(e,n,a)}),callAndCheck(e,function(){return e.uniform1i(r,a)});}function bindCanvasToFramebuffer(e){callAndCheck(e,function(){return e.bindFramebuffer(e.FRAMEBUFFER,null)}),callAndCheck(e,function(){return e.viewport(0,0,e.canvas.width,e.canvas.height)}),callAndCheck(e,function(){return e.scissor(0,0,e.canvas.width,e.canvas.height)});}function bindColorTextureToFramebuffer(e,t,n){callAndCheck(e,function(){return e.bindFramebuffer(e.FRAMEBUFFER,n)}),callAndCheck(e,function(){return e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,t,0)});}function unbindColorTextureFromFramebuffer(e,t){callAndCheck(e,function(){return e.bindFramebuffer(e.FRAMEBUFFER,t)}),callAndCheck(e,function(){return e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,null,0)});}function validateFramebuffer(e){var t=e.checkFramebufferStatus(e.FRAMEBUFFER);if(t!==e.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+getFramebufferErrorMessage(e,t))}function getFramebufferErrorMessage(e,t){switch(t){case e.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:return "FRAMEBUFFER_INCOMPLETE_ATTACHMENT";case e.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:return "FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";case e.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:return "FRAMEBUFFER_INCOMPLETE_DIMENSIONS";case e.FRAMEBUFFER_UNSUPPORTED:return "FRAMEBUFFER_UNSUPPORTED";default:return "unknown error "+t}}function throwIfNull(e,t,n){var r=callAndCheck(e,function(){return t()});if(null==r)throw new Error(n);return r}function validateTextureUnit(e,t){var n=e.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,r=t+e.TEXTURE0;if(rn)throw new Error("textureUnit must be in "+("[gl.TEXTURE0, gl.TEXTURE"+n+"]")+".")}function getTextureShapeFromLogicalShape(e,t){2!==t.length&&(t=squeezeShape(t).newShape);var n=queryMaxTextureSize(e),r=sizeFromShape(t);return t.length<=1&&r<=n?[r,1]:2===t.length&&t[0]<=n&&t[1]<=n?t:3===t.length&&t[0]<=n&&t[1]*t[2]<=n?[t[0],t[1]*t[2]]:4===t.length&&t[0]<=n&&t[1]*t[2]*t[3]<=n?[t[0],t[1]*t[2]*t[3]]:sizeToSquarishShape(r)}var webgl_util=Object.freeze({createWebGLRenderingContext:createWebGLRenderingContext,createWebGLRenderingContextFromCanvas:createWebGLRenderingContextFromCanvas,callAndCheck:callAndCheck,enableDebugWebGLErrorChecking:enableDebugWebGLErrorChecking,checkWebGLError:checkWebGLError,getWebGLErrorMessage:getWebGLErrorMessage,getExtensionOrThrow:getExtensionOrThrow,createVertexShader:createVertexShader,createFragmentShader:createFragmentShader,createProgram:createProgram,linkProgram:linkProgram,validateProgram:validateProgram,createStaticVertexBuffer:createStaticVertexBuffer,createStaticIndexBuffer:createStaticIndexBuffer,queryMaxTextureSize:queryMaxTextureSize,getNumChannels:getNumChannels,createTexture:createTexture,validateTextureSize:validateTextureSize,createFramebuffer:createFramebuffer,bindVertexBufferToProgramAttribute:bindVertexBufferToProgramAttribute,bindTextureUnit:bindTextureUnit,unbindTextureUnit:unbindTextureUnit,getProgramUniformLocationOrThrow:getProgramUniformLocationOrThrow,getProgramUniformLocation:getProgramUniformLocation,bindTextureToProgramUniformSampler:bindTextureToProgramUniformSampler,bindCanvasToFramebuffer:bindCanvasToFramebuffer,bindColorTextureToFramebuffer:bindColorTextureToFramebuffer,unbindColorTextureFromFramebuffer:unbindColorTextureFromFramebuffer,validateFramebuffer:validateFramebuffer,getFramebufferErrorMessage:getFramebufferErrorMessage,getTextureShapeFromLogicalShape:getTextureShapeFromLogicalShape});function getWebGLContextAttributes(){return {alpha:!1,antialias:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1,depth:!1,stencil:!1,failIfMajorPerformanceCaveat:!0}}function createWebGLContext(e){var t,n=getWebGLContextAttributes();return callAndCheck(t=null!=e?createWebGLRenderingContextFromCanvas(e,n):createWebGLRenderingContext(n),function(){return t.disable(t.DEPTH_TEST)}),callAndCheck(t,function(){return t.disable(t.STENCIL_TEST)}),callAndCheck(t,function(){return t.disable(t.BLEND)}),callAndCheck(t,function(){return t.disable(t.DITHER)}),callAndCheck(t,function(){return t.disable(t.POLYGON_OFFSET_FILL)}),callAndCheck(t,function(){return t.disable(t.SAMPLE_COVERAGE)}),callAndCheck(t,function(){return t.enable(t.SCISSOR_TEST)}),callAndCheck(t,function(){return t.enable(t.CULL_FACE)}),callAndCheck(t,function(){return t.cullFace(t.BACK)}),t}function createVertexShader$1(e){return createVertexShader(e,"\n precision highp float;\n attribute vec3 clipSpacePos;\n attribute vec2 uv;\n varying vec2 resultUV;\n\n void main() {\n gl_Position = vec4(clipSpacePos, 1);\n resultUV = uv;\n }")}function createVertexBuffer(e){return createStaticVertexBuffer(e,new Float32Array([-1,1,0,0,1,-1,-1,0,0,0,1,1,0,1,1,1,-1,0,1,0]))}function createIndexBuffer(e){return createStaticIndexBuffer(e,new Uint16Array([0,1,2,2,1,3]))}function getTextureConfig(e,t){var n,r,a,o,i,s,u,l=e;return 2===ENV.get("WEBGL_VERSION")?(n=l.R32F,r=l.R16F,a=l.RGBA32F,o=l.RED,i=4,s=1,u=l.HALF_FLOAT):(n=e.RGBA,r=e.RGBA,a=l.RGBA,o=e.RGBA,i=4,s=4,u=null!=t?t.HALF_FLOAT_OES:null),{internalFormatFloat:n,internalFormatHalfFloat:r,internalFormatPackedFloat:a,textureFormatFloat:o,downloadTextureFormat:e.RGBA,downloadUnpackNumChannels:i,defaultNumChannels:s,textureTypeHalfFloat:u}}function createAndConfigureTexture(e,t,n,r,a,o){validateTextureSize(e,t,n);var i=createTexture(e),s=e.TEXTURE_2D;return callAndCheck(e,function(){return e.bindTexture(s,i)}),callAndCheck(e,function(){return e.texParameteri(s,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE)}),callAndCheck(e,function(){return e.texParameteri(s,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)}),callAndCheck(e,function(){return e.texParameteri(s,e.TEXTURE_MIN_FILTER,e.NEAREST)}),callAndCheck(e,function(){return e.texParameteri(s,e.TEXTURE_MAG_FILTER,e.NEAREST)}),callAndCheck(e,function(){return e.texImage2D(s,0,r,t,n,0,a,o,null)}),callAndCheck(e,function(){return e.bindTexture(e.TEXTURE_2D,null)}),i}function createFloat32MatrixTexture(e,t,n,r){var a=getUnpackedMatrixTextureShapeWidthHeight(t,n);return createAndConfigureTexture(e,a[0],a[1],r.internalFormatFloat,r.textureFormatFloat,e.FLOAT)}function createFloat16MatrixTexture(e,t,n,r){var a=getUnpackedMatrixTextureShapeWidthHeight(t,n);return createAndConfigureTexture(e,a[0],a[1],r.internalFormatFloat,r.textureFormatFloat,r.textureTypeHalfFloat)}function createUnsignedBytesMatrixTexture(e,t,n,r){var a=getUnpackedMatrixTextureShapeWidthHeight(t,n);return createAndConfigureTexture(e,a[0],a[1],e.RGBA,e.RGBA,e.UNSIGNED_BYTE)}function createPackedMatrixTexture(e,t,n,r){var a=getPackedMatrixTextureShapeWidthHeight(t,n);return createAndConfigureTexture(e,a[0],a[1],r.internalFormatPackedFloat,e.RGBA,e.FLOAT)}function bindVertexProgramAttributeStreams(e,t,n){return callAndCheck(e,function(){return e.bindBuffer(e.ARRAY_BUFFER,n)}),bindVertexBufferToProgramAttribute(e,t,"clipSpacePos",n,3,20,0)&&bindVertexBufferToProgramAttribute(e,t,"uv",n,2,20,12)}function uploadPixelDataToTexture(e,t,n){callAndCheck(e,function(){return e.bindTexture(e.TEXTURE_2D,t)}),callAndCheck(e,function(){return e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n)}),callAndCheck(e,function(){return e.bindTexture(e.TEXTURE_2D,null)});}function uploadDataToTexture(e,t,n,r,a,o){validateTextureSize(e,n,r),callAndCheck(e,function(){return e.bindTexture(e.TEXTURE_2D,t)}),callAndCheck(e,function(){return e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,r,o,e.FLOAT,a)}),callAndCheck(e,function(){return e.bindTexture(e.TEXTURE_2D,null)});}function uploadMatrixToTexture(e,t,n,r,a,o,i){var s,u=getUnpackedMatrixTextureShapeWidthHeight(n,r),l=u[0],c=u[1];1===i.defaultNumChannels?s=a:encodeMatrixToUnpackedArray(a,s=new Float32Array(getUnpackedArraySizeFromMatrixSize(a.length,o)),o),uploadDataToTexture(e,t,l,c,s,i.textureFormatFloat);}function uploadMatrixToPackedTexture(e,t,n,r,a,o){var i=getPackedMatrixTextureShapeWidthHeight(n,r),s=i[0],u=i[1],l=new Float32Array(getPackedRGBAArraySizeFromMatrixShape(n,r));encodeMatrixToPackedRGBA(a,n,r,l),uploadDataToTexture(e,t,s,u,l,e.RGBA);}function downloadMatrixFromOutputTextureAsync(e,t,n,r,a){return __awaiter(this,void 0,void 0,function(){var o,i,s,u,l;return __generator(this,function(c){switch(c.label){case 0:return o=e,i=new Float32Array(getUnpackedArraySizeFromMatrixSize(n*r,a.downloadUnpackNumChannels)),s=i instanceof Float32Array?4*i.length:i,u=e.createBuffer(),callAndCheck(e,function(){return e.bindBuffer(o.PIXEL_PACK_BUFFER,u)}),callAndCheck(e,function(){return e.bufferData(o.PIXEL_PACK_BUFFER,s,e.STATIC_DRAW)}),callAndCheck(e,function(){return o.readPixels(0,0,r,n,e.RGBA,e.FLOAT,0)}),[4,t.getBufferSubDataAsync(o.PIXEL_PACK_BUFFER,0,i)];case 1:return c.sent(),l=new Float32Array(n*r),decodeMatrixFromUnpackedArray(i,l,a.downloadUnpackNumChannels),[2,l]}})})}function downloadFloat32MatrixFromOutputTexture(e,t,n,r){var a=getUnpackedMatrixTextureShapeWidthHeight(t,n),o=a[0],i=a[1],s=new Float32Array(getUnpackedArraySizeFromMatrixSize(t*n,r.downloadUnpackNumChannels));callAndCheck(e,function(){return e.readPixels(0,0,o,i,r.downloadTextureFormat,e.FLOAT,s)});var u=new Float32Array(t*n);return decodeMatrixFromUnpackedArray(s,u,r.downloadUnpackNumChannels),u}function downloadByteEncodedFloatMatrixFromOutputTexture(e,t,n,r){var a=getUnpackedMatrixTextureShapeWidthHeight(t,n),o=a[0],i=a[1],s=new Uint8Array(getUnpackedArraySizeFromMatrixSize(t*n,4));return callAndCheck(e,function(){return e.readPixels(0,0,o,i,r.downloadTextureFormat,e.UNSIGNED_BYTE,s)}),new Float32Array(s.buffer)}function downloadMatrixFromPackedOutputTexture(e,t,n,r){var a=getPackedMatrixTextureShapeWidthHeight(t,n),o=a[0],i=a[1],s=new Float32Array(getPackedRGBAArraySizeFromMatrixShape(t,n));callAndCheck(e,function(){return e.readPixels(0,0,o,i,e.RGBA,e.FLOAT,s)});var u=new Float32Array(t*n);return decodeMatrixFromPackedRGBA(s,t,n,u)}var gpgpu_util=Object.freeze({getWebGLContextAttributes:getWebGLContextAttributes,createWebGLContext:createWebGLContext,createVertexShader:createVertexShader$1,createVertexBuffer:createVertexBuffer,createIndexBuffer:createIndexBuffer,getTextureConfig:getTextureConfig,createFloat32MatrixTexture:createFloat32MatrixTexture,createFloat16MatrixTexture:createFloat16MatrixTexture,createUnsignedBytesMatrixTexture:createUnsignedBytesMatrixTexture,createPackedMatrixTexture:createPackedMatrixTexture,bindVertexProgramAttributeStreams:bindVertexProgramAttributeStreams,uploadPixelDataToTexture:uploadPixelDataToTexture,uploadMatrixToTexture:uploadMatrixToTexture,uploadMatrixToPackedTexture:uploadMatrixToPackedTexture,downloadMatrixFromOutputTextureAsync:downloadMatrixFromOutputTextureAsync,downloadFloat32MatrixFromOutputTexture:downloadFloat32MatrixFromOutputTexture,downloadByteEncodedFloatMatrixFromOutputTexture:downloadByteEncodedFloatMatrixFromOutputTexture,downloadMatrixFromPackedOutputTexture:downloadMatrixFromPackedOutputTexture}),GPGPUContext=function(){function e(e){this.outputTexture=null,this.program=null,this.disposed=!1,this.autoDebugValidate=!1,this.vertexAttrsAreBound=!1,this.itemsToPoll=[],this.gl=null!=e?e:createWebGLContext(),1===ENV.get("WEBGL_VERSION")?(this.textureFloatExtension=getExtensionOrThrow(this.gl,"OES_texture_float"),this.colorBufferFloatExtension=this.gl.getExtension("WEBGL_color_buffer_float"),ENV.get("WEBGL_RENDER_FLOAT32_ENABLED")||(this.textureHalfFloatExtension=getExtensionOrThrow(this.gl,"OES_texture_half_float"),this.colorBufferHalfFloatExtension=this.gl.getExtension("EXT_color_buffer_half_float"))):this.colorBufferFloatExtension=getExtensionOrThrow(this.gl,"EXT_color_buffer_float"),this.loseContextExtension=getExtensionOrThrow(this.gl,"WEBGL_lose_context"),ENV.get("WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED")&&(this.getBufferSubDataAsyncExtension=this.gl.getExtension("WEBGL_get_buffer_sub_data_async")),this.vertexBuffer=createVertexBuffer(this.gl),this.indexBuffer=createIndexBuffer(this.gl),this.framebuffer=createFramebuffer(this.gl),this.textureConfig=getTextureConfig(this.gl,this.textureHalfFloatExtension);}return e.prototype.dispose=function(){var e=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 t=this.gl;callAndCheck(t,function(){return t.finish()}),callAndCheck(t,function(){return t.bindFramebuffer(t.FRAMEBUFFER,null)}),callAndCheck(t,function(){return t.deleteFramebuffer(e.framebuffer)}),callAndCheck(t,function(){return t.bindBuffer(t.ARRAY_BUFFER,null)}),callAndCheck(t,function(){return t.deleteBuffer(e.vertexBuffer)}),callAndCheck(t,function(){return t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,null)}),callAndCheck(t,function(){return t.deleteBuffer(e.indexBuffer)}),this.loseContextExtension.loseContext(),this.disposed=!0;}},e.prototype.enableAutomaticDebugValidation=function(e){this.autoDebugValidate=e,enableDebugWebGLErrorChecking(e);},e.prototype.createFloat32MatrixTexture=function(e,t){return this.throwIfDisposed(),createFloat32MatrixTexture(this.gl,e,t,this.textureConfig)},e.prototype.createFloat16MatrixTexture=function(e,t){return this.throwIfDisposed(),createFloat16MatrixTexture(this.gl,e,t,this.textureConfig)},e.prototype.createUnsignedBytesMatrixTexture=function(e,t){return this.throwIfDisposed(),createUnsignedBytesMatrixTexture(this.gl,e,t,this.textureConfig)},e.prototype.uploadPixelDataToTexture=function(e,t){this.throwIfDisposed(),uploadPixelDataToTexture(this.gl,e,t);},e.prototype.createPackedMatrixTexture=function(e,t){return this.throwIfDisposed(),createPackedMatrixTexture(this.gl,e,t,this.textureConfig)},e.prototype.deleteMatrixTexture=function(e){var t=this;this.throwIfDisposed(),this.outputTexture===e&&(unbindColorTextureFromFramebuffer(this.gl,this.framebuffer),this.outputTexture=null),callAndCheck(this.gl,function(){return t.gl.deleteTexture(e)});},e.prototype.uploadMatrixToTexture=function(e,t,n,r){this.throwIfDisposed();var a=getNumChannels();return uploadMatrixToTexture(this.gl,e,t,n,r,a,this.textureConfig)},e.prototype.uploadMatrixToPackedTexture=function(e,t,n,r){return this.throwIfDisposed(),uploadMatrixToPackedTexture(this.gl,e,t,n,r,this.textureConfig)},e.prototype.downloadFloat32MatrixFromOutputTexture=function(e,t,n){var r=this;return this.downloadMatrixDriver(e,function(){return downloadFloat32MatrixFromOutputTexture(r.gl,t,n,r.textureConfig)})},e.prototype.downloadByteEncodedFloatMatrixFromOutputTexture=function(e,t,n){var r=this;return this.downloadMatrixDriver(e,function(){return downloadByteEncodedFloatMatrixFromOutputTexture(r.gl,t,n,r.textureConfig)})},e.prototype.downloadMatrixFromTextureAsync=function(e,t,n){return __awaiter(this,void 0,void 0,function(){var r=this;return __generator(this,function(a){if(null==this.getBufferSubDataAsyncExtension)throw new Error("Cannot download matrix from output texture asynchronously, WEBGL_get_buffer_sub_data_async is not enabled.");return [2,this.downloadMatrixDriverAsync(e,function(){return downloadMatrixFromOutputTextureAsync(r.gl,r.getBufferSubDataAsyncExtension,t,n,r.textureConfig)})]})})},e.prototype.downloadMatrixFromPackedTexture=function(e,t,n){var r=this;return this.downloadMatrixDriver(e,function(){return downloadMatrixFromPackedOutputTexture(r.gl,t,n,r.textureConfig)})},e.prototype.createProgram=function(e){this.throwIfDisposed();var t=this.gl,n=createFragmentShader(t,e),r=createVertexShader$1(t),a=createProgram(t);return callAndCheck(t,function(){return t.attachShader(a,r)}),callAndCheck(t,function(){return t.attachShader(a,n)}),linkProgram(t,a),this.autoDebugValidate&&validateProgram(t,a),this.vertexAttrsAreBound||(this.setProgram(a),this.vertexAttrsAreBound=bindVertexProgramAttributeStreams(t,this.program,this.vertexBuffer)),a},e.prototype.deleteProgram=function(e){var t=this;this.throwIfDisposed(),e===this.program&&(this.program=null),null!=e&&callAndCheck(this.gl,function(){return t.gl.deleteProgram(e)});},e.prototype.setProgram=function(e){var t=this;this.throwIfDisposed(),this.program=e,null!=this.program&&this.autoDebugValidate&&validateProgram(this.gl,this.program),callAndCheck(this.gl,function(){return t.gl.useProgram(e)});},e.prototype.getUniformLocation=function(e,t,n){return void 0===n&&(n=!0),this.throwIfDisposed(),n?getProgramUniformLocationOrThrow(this.gl,e,t):getProgramUniformLocation(this.gl,e,t)},e.prototype.getAttributeLocation=function(e,t){var n=this;return this.throwIfDisposed(),callAndCheck(this.gl,function(){return n.gl.getAttribLocation(e,t)})},e.prototype.getUniformLocationNoThrow=function(e,t){return this.throwIfDisposed(),this.gl.getUniformLocation(e,t)},e.prototype.setInputMatrixTexture=function(e,t,n){this.throwIfDisposed(),this.throwIfNoProgram(),bindTextureToProgramUniformSampler(this.gl,this.program,e,t,n);},e.prototype.setOutputMatrixTexture=function(e,t,n){this.setOutputMatrixTextureDriver(e,n,t);},e.prototype.setOutputPackedMatrixTexture=function(e,t,n){this.throwIfDisposed();var r=getPackedMatrixTextureShapeWidthHeight(t,n),a=r[0],o=r[1];this.setOutputMatrixTextureDriver(e,a,o);},e.prototype.setOutputMatrixWriteRegion=function(e,t,n,r){this.setOutputMatrixWriteRegionDriver(n,e,r,t);},e.prototype.setOutputPackedMatrixWriteRegion=function(e,t,n,r){throw new Error("setOutputPackedMatrixWriteRegion not implemented.")},e.prototype.debugValidate=function(){null!=this.program&&validateProgram(this.gl,this.program),validateFramebuffer(this.gl);},e.prototype.executeProgram=function(){this.throwIfDisposed(),this.throwIfNoProgram();var e=this.gl;this.autoDebugValidate&&this.debugValidate(),callAndCheck(e,function(){return e.drawElements(e.TRIANGLES,6,e.UNSIGNED_SHORT,0)});},e.prototype.blockUntilAllProgramsCompleted=function(){var e=this;this.throwIfDisposed(),callAndCheck(this.gl,function(){return e.gl.finish()});},e.prototype.getQueryTimerExtension=function(){return null==this.disjointQueryTimerExtension&&(this.disjointQueryTimerExtension=getExtensionOrThrow(this.gl,2===ENV.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")?"EXT_disjoint_timer_query_webgl2":"EXT_disjoint_timer_query")),this.disjointQueryTimerExtension},e.prototype.getQueryTimerExtensionWebGL2=function(){return this.getQueryTimerExtension()},e.prototype.getQueryTimerExtensionWebGL1=function(){return this.getQueryTimerExtension()},e.prototype.runQuery=function(e){var t=this.beginQuery();return e(),this.endQuery(),this.pollQueryTime(t)},e.prototype.beginQuery=function(){if(2===ENV.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")){var e=this.gl,t=this.getQueryTimerExtensionWebGL2(),n=e.createQuery();return e.beginQuery(t.TIME_ELAPSED_EXT,n),n}var r=this.getQueryTimerExtensionWebGL1(),a=r.createQueryEXT();return r.beginQueryEXT(r.TIME_ELAPSED_EXT,a),a},e.prototype.endQuery=function(){if(2!==ENV.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")){var e=this.getQueryTimerExtensionWebGL1();e.endQueryEXT(e.TIME_ELAPSED_EXT);}else{var t=this.gl,n=this.getQueryTimerExtensionWebGL2();t.endQuery(n.TIME_ELAPSED_EXT);}},e.prototype.isQueryAvailable=function(e,t){if(0===t)return !0;if(2===t){var n=this.gl,r=this.getQueryTimerExtensionWebGL2(),a=n.getQueryParameter(e,n.QUERY_RESULT_AVAILABLE);return null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),a&&!this.disjoint}a=(r=this.getQueryTimerExtensionWebGL1()).getQueryObjectEXT(e,r.QUERY_RESULT_AVAILABLE_EXT);return null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),a&&!this.disjoint},e.prototype.pollQueryTime=function(e){var t=this;return new Promise(function(n){var r=ENV.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION");t.addItemToPoll(function(){return t.isQueryAvailable(e,r)},function(){return n(t.getQueryTime(e,r))});})},e.prototype.pollItems=function(){for(var e=binSearchLastTrue(this.itemsToPoll.map(function(e){return e.isDoneFn})),t=0;t<=e;++t){(0, this.itemsToPoll[t].resolveFn)();}this.itemsToPoll=this.itemsToPoll.slice(e+1);},e.prototype.addItemToPoll=function(e,t){var n=this;this.itemsToPoll.push({isDoneFn:e,resolveFn:t}),this.itemsToPoll.length>1||repeatedTry(function(){return n.pollItems(),0===n.itemsToPoll.length});},e.prototype.getQueryTime=function(e,t){if(0===t)return null;if(2===t){var n=this.gl;return n.getQueryParameter(e,n.QUERY_RESULT)/1e6}var r=this.getQueryTimerExtensionWebGL1();return r.getQueryObjectEXT(e,r.QUERY_RESULT_EXT)/1e6},e.prototype.downloadMatrixDriverSetup=function(e){this.throwIfDisposed(),bindColorTextureToFramebuffer(this.gl,e,this.framebuffer),this.autoDebugValidate&&validateFramebuffer(this.gl);},e.prototype.downloadMatrixDriverTeardown=function(){null!=this.outputTexture?(bindColorTextureToFramebuffer(this.gl,this.outputTexture,this.framebuffer),this.autoDebugValidate&&validateFramebuffer(this.gl)):unbindColorTextureFromFramebuffer(this.gl,this.framebuffer);},e.prototype.downloadMatrixDriver=function(e,t){this.downloadMatrixDriverSetup(e);var n=t();return this.downloadMatrixDriverTeardown(),n},e.prototype.downloadMatrixDriverAsync=function(e,t){return __awaiter(this,void 0,void 0,function(){var n;return __generator(this,function(r){switch(r.label){case 0:return this.downloadMatrixDriverSetup(e),[4,t()];case 1:return n=r.sent(),this.downloadMatrixDriverTeardown(),[2,n]}})})},e.prototype.setOutputMatrixTextureDriver=function(e,t,n){this.throwIfDisposed();var r=this.gl;bindColorTextureToFramebuffer(r,e,this.framebuffer),this.autoDebugValidate&&validateFramebuffer(r),this.outputTexture=e,callAndCheck(r,function(){return r.viewport(0,0,t,n)}),callAndCheck(r,function(){return r.scissor(0,0,t,n)});},e.prototype.setOutputMatrixWriteRegionDriver=function(e,t,n,r){var a=this;this.throwIfDisposed(),callAndCheck(this.gl,function(){return a.gl.scissor(e,t,n,r)});},e.prototype.throwIfDisposed=function(){if(this.disposed)throw new Error("Attempted to use disposed GPGPUContext.")},e.prototype.throwIfNoProgram=function(){if(null==this.program)throw new Error("No GPU program is currently set.")},e}();function binSearchLastTrue(e){for(var t=0,n=e.length-1,r=-1;t<=n;){var a=t+n>>1;e[a]()?(r=a,t=a+1):n=a-1;}return r}function compileProgram(e,t,n,r){for(var a=t.userCode,o=n.map(function(e,n){var r={logicalShape:e.tensor.shape,texShape:e.isUniform?null:e.texData.texShape,isUniform:e.isUniform};return {name:t.variableNames[n],shapeInfo:r}}),i=o.map(function(e){return e.shapeInfo}),s={logicalShape:r.tensor.shape,texShape:r.texData.texShape,isUniform:!1},u=makeShader(o,s,a,!0===t.supportsBroadcasting),l=e.createProgram(u),c={},d=0;d4)throw Error("Where for rank "+n+" is not yet supported");if(1===n)a="resRC",r="resRC";else{for(var o=["resRC.x","resRC.y","resRC.z","resRC.w"],i=[],s=[],u=0;u= 1.0) {\n setOutput(getA("+a+"));\n } else {\n setOutput(getB("+a+"));\n }\n }\n ";}}(),LRNProgram=function(){return function(e,t,n,r,a){this.variableNames=["x"],this.outputShape=[];var o,i=t,s=e[3]-1;this.outputShape=e;var u="float("+n+") + float("+r+") * sum";o=.5===a?"inversesqrt("+u+")":1===a?"1.0/("+u+")":"exp(log("+u+") * float(-"+a+"));",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 <= "+s+") {\n float z = getX(b, r, c, idx);\n sum += z * z;\n }\n }\n float val = x * "+o+";\n setOutput(val);\n }\n ";}}(),MaxPool2DBackpropProgram=function(){return function(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,o=t-1-e.padInfo.top,i=n-1-e.padInfo.left,s=t*n-1;this.userCode="\n const ivec2 pads = ivec2("+o+", "+i+");\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 < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.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 * "+n+" + wC;\n float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);\n\n dotProd += dyValue * mask;\n }\n }\n setOutput(dotProd);\n }\n ";}}(),MatMulProgram=function(){return function(e,t,n,r){void 0===n&&(n=!1),void 0===r&&(r=!1),this.variableNames=["matrixA","matrixB"];var a=n?e[1]:e[0],o=r?t[0]:t[1],i=n?e[0]:e[1];this.outputShape=[a,o];var s=function(e,t){return n?t+" + "+e+", aRow":"aRow, "+t+" + "+e},u=function(e,t){return r?"bCol, "+t+" + "+e:t+" + "+e+", bCol"},l=4*Math.floor(i/4),c=i%4;this.userCode=" float dotARowBCol(int aRow, int bCol) {\n float result = 0.0;\n for (int i = 0; i < "+l+"; i += 4) {\n vec4 a = vec4(\n getMatrixA("+s(0,"i")+"),\n getMatrixA("+s(1,"i")+"),\n getMatrixA("+s(2,"i")+"),\n getMatrixA("+s(3,"i")+")\n );\n vec4 b = vec4(\n getMatrixB("+u(0,"i")+"),\n getMatrixB("+u(1,"i")+"),\n getMatrixB("+u(2,"i")+"),\n getMatrixB("+u(3,"i")+")\n );\n\n result += dot(a, b);\n }\n\n if ("+(1===c)+") {\n result += getMatrixA("+s(0,l)+") *\n getMatrixB("+u(0,l)+");\n } else if ("+(2===c)+") {\n vec2 a = vec2(\n getMatrixA("+s(0,l)+"),\n getMatrixA("+s(1,l)+")\n );\n vec2 b = vec2(\n getMatrixB("+u(0,l)+"),\n getMatrixB("+u(1,l)+")\n );\n result += dot(a, b);\n } else if ("+(3===c)+") {\n vec3 a = vec3(\n getMatrixA("+s(0,l)+"),\n getMatrixA("+s(1,l)+"),\n getMatrixA("+s(2,l)+")\n );\n vec3 b = vec3(\n getMatrixB("+u(0,l)+"),\n getMatrixB("+u(1,l)+"),\n getMatrixB("+u(2,l)+")\n );\n result += dot(a, b);\n }\n\n return result;\n }\n\n void main() {\n ivec2 resRC = getOutputCoords();\n setOutput(dotARowBCol(resRC.x, resRC.y));\n }\n ";}}(),MultinomialProgram=function(){function e(e,t,n){this.variableNames=["probs"],this.outputShape=[e,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 < "+(t-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("+(t-1)+"));\n }\n ";}return e.prototype.getCustomSetupFunc=function(e){var t=this;return function(n,r){null==t.seedLoc&&(t.seedLoc=n.getUniformLocation(r,"seed")),n.gl.uniform1f(t.seedLoc,e);}},e}(),OneHotProgram=function(){return function(e,t,n,r){this.variableNames=["indices"],this.outputShape=[e,t],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 ";}}(),PadProgram=function(){return function(e,t,n){this.variableNames=["x"],this.outputShape=t.map(function(t,n){return t[0]+e[n]+t[1]});var r=e.length,a=getCoordsDataType(r),o=t.map(function(e){return e[0]}).join(","),i=t.map(function(t,n){return t[0]+e[n]}).join(","),s=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,r);this.userCode=1!==r?"\n "+a+" start = "+a+"("+o+");\n "+a+" end = "+a+"("+i+");\n\n void main() {\n "+a+" outC = getOutputCoords();\n if (any(lessThan(outC, start)) || any(greaterThanEqual(outC, end))) {\n setOutput(float("+n+"));\n } else {\n "+a+" coords = outC - start;\n setOutput(getX("+s+"));\n }\n }\n ":"\n int start = "+o+";\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 ";}}(),Pool2DProgram=function(){return function(e,t,n){if(this.variableNames=["x"],"avg"===t&&n)throw new Error("Cannot compute positions for average pool.");var r=e.filterHeight,a=e.filterWidth,o=e.strideHeight,i=e.strideWidth,s=e.padInfo.top,u=e.padInfo.left;this.outputShape=e.outShape;var l="avg"===t,c="0.0";if(l||(c="-1.0 / 0.0"),n)this.userCode="\n const ivec2 strides = ivec2("+o+", "+i+");\n const ivec2 pads = ivec2("+s+", "+u+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n float minMaxValue = 0.0;\n float minMaxValueFound = 0.0;\n int minMaxPosition = 0;\n float avgValue = 0.0;\n\n for (int wR = 0; wR < "+r+"; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+a+"; wC++) {\n int xC = xCCorner + wC;\n\n if (xC < 0 || xC >= "+e.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 * "+a+" + wC;\n }\n }\n }\n setOutput(float(minMaxPosition));\n }\n ";else{var d=t+"("+t+"("+t+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"avg"===t&&(d="avgValue / count");var p=4*Math.floor(a/4),h=a%4,f="\n if ("+l+") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = max(values, minMaxValue);\n }\n ";this.userCode="\n const ivec2 strides = ivec2("+o+", "+i+");\n const ivec2 pads = ivec2("+s+", "+u+");\n const float initializationValue = "+c+";\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 >= "+e.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("+c+");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wR = 0; wR < "+r+"; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+p+"; wC += 4) {\n int xC = xCCorner + wC;\n\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n getValue(batch, xR, xC + 3, d)\n );\n\n "+f+"\n }\n\n int xC = xCCorner + "+p+";\n if ("+(1===h)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(2===h)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(3===h)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n initializationValue\n );\n\n "+f+"\n }\n }\n setOutput("+d+");\n }\n ";}}}(),ReduceProgram=function(){return function(e,t){this.variableNames=["x"];var n=e.windowSize,r=e.batchSize,a=e.inSize,o=Math.ceil(a/n);this.outputShape=[r,o];var i="0.0",s="";"min"===t?(i="1.0 / 0.0",s="min"):"max"===t&&(i="-1.0 / 0.0",s="max");var u=t+"("+t+"("+t+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"sum"===t?u="sumValue":"all"===t&&(u="allValue");var l=4*Math.floor(n/4),c=n%4,d="\n if ("+("sum"===t)+") {\n sumValue += dot(values, ones);\n } else {\n minMaxValue = "+s+"(values, minMaxValue);\n }\n ",p="vec4";"all"===t&&(i="1.0",d="\n bool reducedAllValue = all(values);\n float floatedReducedAllValue = float(reducedAllValue);\n allValue = float(allValue >= 1.0 && floatedReducedAllValue >= 1.0);\n ",p="bvec4");var h="";a%n>0&&(h="\n if (inIdx < 0 || inIdx >= "+a+") {\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 "+h+"\n return getX(batch, inIdx);\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * "+n+";\n\n vec4 minMaxValue = vec4("+i+");\n float sumValue = 0.0;\n float allValue = 1.0;\n\n for (int i = 0; i < "+l+"; i += 4) {\n int inIdx = inOffset + i;\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n "+d+"\n }\n\n int inIdx = inOffset + "+l+";\n if ("+(1===c)+") {\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+d+"\n } else if ("+(2===c)+") {\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n "+d+"\n } else if ("+(3===c)+") {\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n "+d+"\n }\n setOutput("+u+");\n }\n ";}}(),ResizeBilinearBackpropProgram=function(){return function(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t.shape;var r=t.shape,a=r[1],o=r[2],i=e.shape,s=i[1],u=i[2],l=[n&&s>1?a-1:a,n&&u>1?o-1:o],c=[n&&s>1?s-1:s,n&&u>1?u-1:u],d=l[0]/c[0],p=l[1]/c[1],h=1/d,f=1/p,g=2*Math.ceil(h)+2,m=2*Math.ceil(f)+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("+d+");\n const float widthScale = float("+p+");\n\n const float invHeightScale = float("+h+");\n const float invWidthScale = float("+f+");\n\n const int winHeight = int("+g+");\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 >= "+s+") {\n continue;\n }\n\n for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {\n int dyC = dyCOffset + startDyC;\n\n // Guard against the window exceeding the bounds of dy\n if (dyC < 0 || dyC >= "+u+") {\n continue;\n }\n\n float dxR = float(dyR) * heightScale;\n int topDxRIndex = int(floor(dxR));\n int bottomDxRIndex = int(min(ceil(dxR), "+(a-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), "+(o-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 ";}}(),ResizeBilinearProgram=function(){return function(e,t,n,r){this.variableNames=["A"],this.outputShape=[];var a=e[0],o=e[1],i=e[2],s=e[3];this.outputShape=[a,t,n,s];var u=[r&&t>1?o-1:o,r&&n>1?i-1:i],l=[r&&t>1?t-1:t,r&&n>1?n-1:n];this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+u[0]/l[0]+",\n "+u[1]/l[1]+");\n const vec2 inputShapeRC = vec2("+o+".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 ";}}(),ResizeNearestNeighborProgram=function(){return function(e,t,n,r){this.variableNames=["A"],this.outputShape=[];var a=e[0],o=e[1],i=e[2],s=e[3];this.outputShape=[a,t,n,s];var u=r?[o-1,i-1]:[o,i],l=r?[t-1,n-1]:[t,n],c=r?"0.5":"0.0";this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+u[0]/l[0]+",\n "+u[1]/l[1]+");\n const vec2 inputShapeRC = vec2("+o+".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 + "+c+")));\n\n float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);\n\n setOutput(newValue);\n }\n ";}}(),ReverseProgram=function(){return function(e,t){this.variableNames=["x"];var n=e.length;if(n>4)throw new Error("WebGL backend: Reverse of rank-"+n+" tensor is not yet supported");if(this.outputShape=e,1!==n){var r=e.map(function(n,r){return function(n){return -1!==t.indexOf(n)&&1!==e[n]?e[n]+" - coords["+n+"] - 1":"coords["+n+"]"}(r)}).join(","),a=getCoordsDataType(n);this.userCode="\n void main() {\n "+a+" coords = getOutputCoords();\n setOutput(getX("+r+"));\n }\n ";}else this.userCode="\n void main() {\n int coord = getOutputCoords();\n setOutput(getX("+e[0]+" - coord - 1));\n }\n ";}}(),SegmentOpProgram=function(){return function(e,t){this.variableNames=["x","segmentIds"];var n=e.windowSize,r=e.batchSize,a=e.inSize,o=e.numSegments,i=o*Math.ceil(a/n);this.outputShape=[r,i];var s=4*Math.floor(n/4),u=n%4,l="\n sumValue += dot(values, filter);\n ",c="";a%n>0&&(c="\n if (inIdx < 0 || inIdx >= "+a+") {\n return initializationValue;\n }\n ");var d="";a%n>0&&(d="\n if (inIdx < 0 || inIdx >= "+a+") {\n return -1.0;\n }\n "),this.userCode="\n const float initializationValue = 0.0;\n\n float getValue(int batch, int inIdx) {\n "+c+"\n return getX(batch, inIdx);\n }\n\n float getSegmentIdAtIndex(int inIdx) {\n "+d+"\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 "+o+")) * float("+n+"));\n int currentSeg = int(mod(float(outIdx), float("+o+")));\n\n float sumValue = 0.0;\n\n for (int i = 0; i < "+s+"; i += 4) {\n int inIdx = inOffset + i;\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n vec4 filter = 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 "+l+"\n }\n\n int inIdx = inOffset + "+s+";\n if ("+(1===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n int inIdxSeg = int(getSegmentIdAtIndex(inIdx));\n\n vec4 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n 0,\n 0,\n 0\n );\n\n "+l+"\n } else if ("+(2===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n vec4 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n 0,\n 0\n );\n\n "+l+"\n } else if ("+(3===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n vec4 filter = 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 "+l+"\n }\n setOutput(sumValue);\n }\n ";}}(),SliceProgram=function(){function e(e){this.variableNames=["source"],this.outputShape=e,this.rank=e.length;var t=getCoordsDataType(this.rank),n=getCoords$1(this.rank);this.userCode="\n uniform "+t+" start;\n\n void main() {\n "+t+" sourceLoc = start + getOutputCoords();\n setOutput(getSource("+n+"));\n }\n ";}return e.prototype.getCustomSetupFunc=function(e){var t=this;if(e.length!==this.rank)throw Error("The rank ("+this.rank+") of the program must match the length of start ("+e.length+")");return function(n,r){if(null!=t.startLoc||(t.startLoc=n.getUniformLocationNoThrow(r,"start"),null!=t.startLoc))if(1===t.rank)n.gl.uniform1i(t.startLoc,e[0]);else if(2===t.rank)n.gl.uniform2i(t.startLoc,e[0],e[1]);else if(3===t.rank)n.gl.uniform3i(t.startLoc,e[0],e[1],e[2]);else{if(4!==t.rank)throw Error("Slicing for rank "+t.rank+" is not yet supported");n.gl.uniform4i(t.startLoc,e[0],e[1],e[2],e[3]);}}},e}();function getCoords$1(e){if(1===e)return "sourceLoc";if(2===e)return "sourceLoc.x, sourceLoc.y";if(3===e)return "sourceLoc.x, sourceLoc.y, sourceLoc.z";if(4===e)return "sourceLoc.x, sourceLoc.y, sourceLoc.z, sourceLoc.w";throw Error("Slicing for rank "+e+" is not yet supported")}var StridedSliceProgram=function(){return function(e,t,n){this.variableNames=["x"],this.outputShape=n,this.rank=n.length;var r=getCoordsDataType(this.rank),a="";a=1===this.rank?"coords * strides + begin":n.map(function(e,t){return "coords["+t+"] * strides["+t+"] + begin["+t+"]"}).join(","),this.userCode="\n "+r+" begin = "+r+"("+e+");\n "+r+" strides = "+r+"("+t+");\n\n void main() {\n "+r+" coords = getOutputCoords();\n setOutput(getX("+a+"));\n }\n ";}}(),TextureManager=function(){function e(e){this.gpgpu=e,this.numUsedTextures=0,this.numFreeTextures=0,this.freeTextures={},this.logEnabled=!1,this.usedTextures={};}return e.prototype.acquireTexture=function(e,t){var n,r=getPhysicalFromLogicalTextureType(t),a=getKeyFromTextureShape(e,r);if(a in this.freeTextures||(this.freeTextures[a]=[]),a in this.usedTextures||(this.usedTextures[a]=[]),this.freeTextures[a].length>0){this.numFreeTextures--,this.numUsedTextures++,this.log();var o=this.freeTextures[a].shift();return this.usedTextures[a].push(o),o}return this.numUsedTextures++,this.log(),r===PhysicalTextureType.FLOAT32?n=this.gpgpu.createFloat32MatrixTexture(e[0],e[1]):r===PhysicalTextureType.FLOAT16?n=this.gpgpu.createFloat16MatrixTexture(e[0],e[1]):r===PhysicalTextureType.UNSIGNED_BYTE&&(n=this.gpgpu.createUnsignedBytesMatrixTexture(e[0],e[1])),this.usedTextures[a].push(n),n},e.prototype.releaseTexture=function(e,t,n){var r=getKeyFromTextureShape(t,getPhysicalFromLogicalTextureType(n));r in this.freeTextures||(this.freeTextures[r]=[]),this.freeTextures[r].push(e),this.numFreeTextures++,this.numUsedTextures--;var a=this.usedTextures[r],o=a.indexOf(e);if(o<0)throw new Error("Cannot release a texture that was never provided by this texture manager");a.splice(o,1),this.log();},e.prototype.log=function(){if(this.logEnabled){var e=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used",this.numFreeTextures+" / "+this.numUsedTextures,"("+e+")");}},e.prototype.getNumUsedTextures=function(){return this.numUsedTextures},e.prototype.getNumFreeTextures=function(){return this.numFreeTextures},e.prototype.dispose=function(){var e=this;if(null!=this.freeTextures){for(var t in this.freeTextures)this.freeTextures[t].forEach(function(t){e.gpgpu.deleteMatrixTexture(t);});for(var t in this.usedTextures)this.usedTextures[t].forEach(function(t){e.gpgpu.deleteMatrixTexture(t);});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0;}},e}();function getPhysicalFromLogicalTextureType(e){if(e===TextureUsage.DOWNLOAD||e===TextureUsage.PIXELS)return PhysicalTextureType.UNSIGNED_BYTE;if(e===TextureUsage.UPLOAD)return PhysicalTextureType.FLOAT32;if(e===TextureUsage.RENDER)return ENV.get("WEBGL_RENDER_FLOAT32_ENABLED")?PhysicalTextureType.FLOAT32:PhysicalTextureType.FLOAT16;throw new Error("Unknown logical texture type "+e)}function getKeyFromTextureShape(e,t){return e[0]+"_"+e[1]+"_"+t}var TileProgram=function(){return function(e,t){this.variableNames=["A"];for(var n=new Array(e.length),r=0;r5)throw Error("Tile for rank "+t+" is not yet supported");if(1===t)return "imod(resRC, "+e[0]+")";for(var n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u"],r=[],a=0;a6)throw Error("Transpose for rank "+t+" is not yet supported");for(var n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u","resRC.v"],r=new Array(t),a=0;a= 0.0) ? x : (exp(x) - 1.0);",SELU="\n // Stable and Attracting Fixed Point (0, 1) for Normalized Weights.\n // see: https://arxiv.org/abs/1706.02515\n float scaleAlpha = "+SELU_SCALEALPHA+";\n float scale = "+SELU_SCALE+";\n return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);\n";function STEP(e){return void 0===e&&(e=0),CHECK_NAN_SNIPPET$1+"\n return x > 0.0 ? 1.0 : float("+e+");\n "}var NEG="return -x;",CEIL="return ceil(x);",FLOOR="return floor(x);",SIGN="\n if (isNaN(x)) { return 0.0; }\n return sign(x);\n",ROUND="\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",EXP="return exp(x);",EXPM1="return exp(x) - 1.0;",LOG="return log(x);",LOG1P="return log(1.0 + x);",SQRT="return sqrt(x);",RSQRT="return inversesqrt(x);",SIGMOID="return 1.0 / (1.0 + exp(-1.0 * x));",SOFTPLUS="\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",SIN=CHECK_NAN_SNIPPET$1+"\n return sin(x);\n",COS=CHECK_NAN_SNIPPET$1+"\n return cos(x);\n",TAN="return tan(x);",ASIN="return asin(x);",ACOS="return acos(x);",ATAN=CHECK_NAN_SNIPPET$1+"\n return atan(x);\n",SINH="\n float e2x = exp(x);\n return (e2x - 1.0 / e2x) / 2.0;\n",COSH="\n float e2x = exp(-x);\n return (e2x + 1.0 / e2x) / 2.0;\n",TANH="\n float e2x = exp(-2.0 * abs(x));\n return sign(x) * (1.0 - e2x) / (1.0 + e2x);\n",ASINH="return log(x + sqrt(x * x + 1.0));",ACOSH="return log(x + sqrt(x * x - 1.0));",ATANH="return (log(1.0 + x) - log(1.0 - x)) / 2.0;",ERF='\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 = '+ERF_P+";\n float a1 = "+ERF_A1+";\n float a2 = "+ERF_A2+";\n float a3 = "+ERF_A3+";\n float a4 = "+ERF_A4+";\n float a5 = "+ERF_A5+";\n\n float t = 1.0 / (1.0 + p * x);\n return 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);\n",SQUARE="return x * x;",RECIPROCAL="return 1.0 / x;",LOGICAL_NOT="return float(!(x >= 1.0));",TO_INT="return float(int(x));",BEFORE_PAGING_CONSTANT=300,SIZE_UPLOAD_UNIFORM=32,MathBackendWebGL=function(){function e(e,t){if(void 0===t&&(t=!0),this.gpgpu=e,this.delayedStorage=t,this.texData=new WeakMap,this.pendingRead=new WeakMap,this.pendingDisposal=new WeakSet,this.lruDataGPU=[],this.numBytesInGPU=0,this.uploadWaitMs=0,this.downloadWaitMs=0,this.binaryCache={},this.disposed=!1,ENV.get("WEBGL_VERSION")<1)throw new Error("WebGL is not supported on this device");ENV.get("IS_BROWSER")&&(this.canvas=document.createElement("canvas")),null==e?(this.gpgpu=new GPGPUContext(createWebGLContext(this.canvas)),this.gpgpuCreatedLocally=!0):this.gpgpuCreatedLocally=!1,this.NUM_BYTES_BEFORE_PAGING=window.screen.height*window.screen.width*window.devicePixelRatio*BEFORE_PAGING_CONSTANT,this.textureManager=new TextureManager(this.gpgpu);}return e.prototype.register=function(e,t,n){if(this.texData.has(e))throw new Error("Data buffer is already registered");this.texData.set(e,{shape:t,dtype:n,values:null,texture:null,texShape:null,usage:TextureUsage.RENDER});},e.prototype.fromPixels=function(e,t){if(null==e)throw new Error("MathBackendWebGL.writePixels(): pixels can not be null");var n=[e.height,e.width],r=[e.height,e.width,t];if(e instanceof HTMLVideoElement){if(null==this.fromPixelsCanvas){if(!ENV.get("IS_BROWSER"))throw new Error("Can't read pixels from HTMLImageElement outside the browser.");if("complete"!==document.readyState)throw new Error("The DOM is not ready yet. Please call tf.fromPixels() once the DOM is ready. One way to do that is to add an event listener for `DOMContentLoaded` on the document object");this.fromPixelsCanvas=document.createElement("canvas");}this.fromPixelsCanvas.width=e.width,this.fromPixelsCanvas.height=e.height,this.fromPixelsCanvas.getContext("2d").drawImage(e,0,0,e.width,e.height),e=this.fromPixelsCanvas;}var a=Tensor.make(n,{},"int32");this.texData.get(a.dataId).usage=TextureUsage.PIXELS,this.gpgpu.uploadPixelDataToTexture(this.getTexture(a.dataId),e);var o=new FromPixelsProgram(r),i=this.compileAndRun(o,[a]);return a.dispose(),i},e.prototype.write=function(e,t){if(null==t)throw new Error("MathBackendWebGL.write(): values can not be null");this.throwIfNoData(e);var n=this.texData.get(e),r=n.texture,a=n.texShape,o=n.usage;null!=r&&(this.releaseTexture(e,r,a,o),n.texture=null,n.texShape=null),n.usage=TextureUsage.UPLOAD,n.values=t,this.delayedStorage||this.uploadToGPU(e);},e.prototype.readSync=function(e){this.throwIfNoData(e);var t=this.texData.get(e),n=t.shape,r=t.texture,a=t.values,o=t.texShape,i=t.dtype;if(null!=a)return this.cacheOnCPU(e),a;var s,u,l=null!=this.activeTimers;if(l&&(s=performance.now()),ENV.get("WEBGL_DOWNLOAD_FLOAT_ENABLED"))u=this.gpgpu.downloadFloat32MatrixFromOutputTexture(r,o[0],o[1]);else{var c=Tensor.make(n,{});this.texData.get(c.dataId).usage=TextureUsage.DOWNLOAD;var d=Tensor.make(n,{dataId:e},i),p=new EncodeFloatProgram(n),h=this.compileAndRun(p,[d],c),f=this.texData.get(c.dataId);u=this.gpgpu.downloadByteEncodedFloatMatrixFromOutputTexture(f.texture,f.texShape[0],f.texShape[1]),h.dispose(),d.dispose(),c.dispose();}return l&&(this.downloadWaitMs+=performance.now()-s),this.cacheOnCPU(e,u),t.values},e.prototype.read=function(e){return __awaiter(this,void 0,void 0,function(){var t,n,r,a,o,i,s,u;return __generator(this,function(l){switch(l.label){case 0:return this.pendingRead.has(e)?(t=this.pendingRead.get(e),[2,new Promise(function(e){return t.push(e)})]):(this.throwIfNoData(e),n=this.texData.get(e),r=n.texture,a=n.values,o=n.texShape,null!=a?(this.cacheOnCPU(e),[2,a]):ENV.get("WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED")?[4,this.gpgpu.downloadMatrixFromTextureAsync(r,o[0],o[1])]:[3,2]);case 1:return i=l.sent(),this.cacheOnCPU(e,i),[2,n.values];case 2:return 0===ENV.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")?[2,this.readSync(e)]:(this.pendingRead.set(e,[]),[4,this.gpgpu.runQuery(function(){})]);case 3:return l.sent(),s=this.pendingRead.get(e),this.pendingRead.delete(e),u=this.readSync(e),s.forEach(function(e){return e(u)}),this.pendingDisposal.has(e)&&(this.pendingDisposal.delete(e),this.disposeData(e)),[2,u]}})})},e.prototype.time=function(e){return __awaiter(this,void 0,void 0,function(){var t,n,r,a,o,i;return __generator(this,function(s){switch(s.label){case 0:return t=this.activeTimers,n=[],r=!1,null==this.programTimersStack?(this.programTimersStack=n,r=!0):this.activeTimers.push(n),this.activeTimers=n,e(),a=flatten(this.activeTimers),this.activeTimers=t,r&&(this.programTimersStack=null),[4,Promise.all(a).then(function(e){var t=0;return e.forEach(function(e){return t+=e}),t})];case 1:return o=s.sent(),i={uploadWaitMs:this.uploadWaitMs,downloadWaitMs:this.downloadWaitMs,kernelMs:o,wallMs:null},this.uploadWaitMs=0,this.downloadWaitMs=0,[2,i]}})})},e.prototype.memory=function(){return {unreliable:!1,numBytesInGPU:this.numBytesInGPU}},e.prototype.startTimer=function(){return ENV.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?this.gpgpu.beginQuery():{startMs:performance.now(),endMs:null}},e.prototype.endTimer=function(e){return ENV.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?(this.gpgpu.endQuery(),e):(e.endMs=performance.now(),e)},e.prototype.getQueryTime=function(e){return __awaiter(this,void 0,void 0,function(){var t;return __generator(this,function(n){return ENV.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?[2,this.gpgpu.pollQueryTime(e)]:[2,(t=e).endMs-t.startMs]})})},e.prototype.disposeData=function(e){if(!this.pendingDisposal.has(e))if(this.pendingRead.has(e))this.pendingDisposal.add(e);else if(this.texData.has(e)){var t=this.texData.get(e),n=t.texture,r=t.texShape,a=t.usage;null!=n&&this.releaseTexture(e,n,r,a),this.texData.delete(e);}},e.prototype.getTexture=function(e){return this.uploadToGPU(e),this.texData.get(e).texture},e.prototype.getGPGPUContext=function(){return this.gpgpu},e.prototype.getCanvas=function(){return this.canvas},e.prototype.slice=function(e,t,n){var r=new SliceProgram(n),a=r.getCustomSetupFunc(t);return this.compileAndRun(r,[e],null,a)},e.prototype.stridedSlice=function(e,t,n,r,a,o){var i=getStridedSlicedInfo(e.shape,t,n,r,a,o),s=i[0],u=i[1];if(u.some(function(e){return 0===e}))return tensor([],u);var l=new StridedSliceProgram(s,r,u);return this.compileAndRun(l,[e])},e.prototype.reverse=function(e,t){var n=new ReverseProgram(e.shape,t);return this.compileAndRun(n,[e])},e.prototype.concat=function(e,t){var n=new ConcatProgram(e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.neg=function(e){var t=new UnaryOpProgram(e.shape,NEG);return this.compileAndRun(t,[e])},e.prototype.matMul=function(e,t,n,r){var a=new MatMulProgram(e.shape,t.shape,n,r);return this.compileAndRun(a,[e,t])},e.prototype.multiply=function(e,t){var n=new BinaryOpProgram(MUL,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,upcastType(e.dtype,t.dtype));return this.compileAndRun(n,[e,t],r)},e.prototype.batchNormalization=function(e,t,n,r,a,o){var i=[e,t,n],s=null;null!=o&&(s=o.shape,i.push(o));var u=null;null!=a&&(u=a.shape,i.push(a));var l=new BatchNormProgram(e.shape,t.shape,n.shape,s,u,r);return this.compileAndRun(l,i)},e.prototype.localResponseNormalization4D=function(e,t,n,r,a){var o=new LRNProgram(e.shape,t,n,r,a);return this.compileAndRun(o,[e])},e.prototype.tile=function(e,t){var n=new TileProgram(e.shape,t);return this.compileAndRun(n,[e])},e.prototype.pad=function(e,t,n){var r=new PadProgram(e.shape,t,n);return this.compileAndRun(r,[e])},e.prototype.transpose=function(e,t){var n=new TransposeProgram(e.shape,t);return this.compileAndRun(n,[e])},e.prototype.gather=function(e,t,n){var r=new GatherProgram(e.shape,t.size,n);return this.compileAndRun(r,[e,t])},e.prototype.reduce=function(e,t,n){var r=e.shape[0],a=e.shape[1],o=computeOptimalWindowSize(a),i=new ReduceProgram({windowSize:o,inSize:a,batchSize:r},t),s=i.outputShape,u=s[0],l=s[1],c=this.makeOutputArray([u,l],n);return this.compileAndRun(i,[e],c),1===c.shape[1]?c:this.reduce(c,t,n)},e.prototype.argReduce=function(e,t,n){void 0===n&&(n=null);var r=e.shape[0],a=e.shape[1];null!=n&&(r=n.shape[0],a=n.shape[1]);var o=computeOptimalWindowSize(a),i=new ArgMinMaxProgram({windowSize:o,inSize:a,batchSize:r},t,null==n),s=i.outputShape,u=s[0],l=s[1],c=this.makeOutputArray([u,l],"int32"),d=[e];return null!=n&&d.push(n),this.compileAndRun(i,d,c),1===c.shape[1]?c:this.argReduce(e,t,c)},e.prototype.sum=function(e,t){assertAxesAreInnerMostDims("sum",t,e.rank);var n=computeOutAndReduceShapes(e.shape,t),r=n[0],a=sizeFromShape(n[1]),o=e.as2D(-1,a),i=sumOutType(e.dtype);return this.reduce(o,"sum",i).reshape(r)},e.prototype.unsortedSegmentSum=function(e,t,n){var r=0,a=getAxesPermutation([r],e.rank),o=e;null!=a&&(o=e.transpose(a),r=getInnerMostAxes(1,e.rank)[0]);var i=computeOutShape$1(o.shape,r,n),s=sizeFromShape([o.shape[r]]),u=o.as2D(-1,s),l=sumOutType(e.dtype),c=this.segOpCompute(u,"unsortedSegmentSum",t,l,n).reshape(i);return null!=a&&(c=c.transpose(getUndoAxesPermutation(a))),c},e.prototype.segOpCompute=function(e,t,n,r,a){var o=e.shape[0],i=e.shape[1],s=segOpComputeOptimalWindowSize(i,a),u=new SegmentOpProgram({windowSize:s,inSize:i,batchSize:o,numSegments:a},t),l=u.outputShape,c=l[0],d=l[1],p=this.makeOutputArray([c,d],r);return this.compileAndRun(u,[e,n],p),p.shape[1]===a?p:(n=range(0,a).tile([i/s]),this.segOpCompute(p,t,n,r,a))},e.prototype.argMin=function(e,t){var n=[t];assertAxesAreInnerMostDims("argMin",n,e.rank);var r=computeOutAndReduceShapes(e.shape,n),a=r[0],o=sizeFromShape(r[1]),i=e.as2D(-1,o);return this.argReduce(i,"min").reshape(a)},e.prototype.argMax=function(e,t){var n=[t];assertAxesAreInnerMostDims("argMax",n,e.rank);var r=computeOutAndReduceShapes(e.shape,n),a=r[0],o=sizeFromShape(r[1]),i=e.as2D(-1,o);return this.argReduce(i,"max").reshape(a)},e.prototype.cumsum=function(e,t,n,r){if(t!==e.rank-1)throw new Error("WebGL cumsum shader expects an inner-most axis="+(e.rank-1)+" but got axis="+t);var a=new CumSumProgram(e.shape,n,r);return this.compileAndRun(a,[e])},e.prototype.equal=function(e,t){var n=new BinaryOpProgram(EQUAL,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.notEqual=function(e,t){var n=new BinaryOpProgram(NOT_EQUAL,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.less=function(e,t){var n=new BinaryOpProgram(LESS,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.lessEqual=function(e,t){var n=new BinaryOpProgram(LESS_EQUAL,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.greater=function(e,t){var n=new BinaryOpProgram(GREATER,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.greaterEqual=function(e,t){var n=new BinaryOpProgram(GREATER_EQUAL,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.logicalNot=function(e){var t=new UnaryOpProgram(e.shape,LOGICAL_NOT);return this.compileAndRun(t,[e])},e.prototype.logicalAnd=function(e,t){var n=new BinaryOpProgram(LOGICAL_AND,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.logicalOr=function(e,t){var n=new BinaryOpProgram(LOGICAL_OR,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.where=function(e,t,n,r){var a=new WhereProgram(e.rank,t.shape,t.rank),o=this.makeOutputArray(a.outputShape,r);return this.compileAndRun(a,[e,t,n],o)},e.prototype.topKValues=function(e,t){throw new Error("topKValues GPU not yet implemented!")},e.prototype.topKIndices=function(e,t){throw new Error("topKIndices GPU not yet implemented!")},e.prototype.min=function(e,t){assertAxesAreInnerMostDims("min",t,e.rank);var n=computeOutAndReduceShapes(e.shape,t),r=n[0],a=sizeFromShape(n[1]),o=e.as2D(-1,a);return this.reduce(o,"min",o.dtype).reshape(r)},e.prototype.minimum=function(e,t){var n=new BinaryOpProgram(MIN,e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.mod=function(e,t){var n=new BinaryOpProgram(MOD,e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.max=function(e,t){assertAxesAreInnerMostDims("max",t,e.rank);var n=computeOutAndReduceShapes(e.shape,t),r=n[0],a=sizeFromShape(n[1]),o=e.as2D(-1,a);return this.reduce(o,"max",o.dtype).reshape(r)},e.prototype.maximum=function(e,t){var n=new BinaryOpProgram(MAX,e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.all=function(e,t){assertAxesAreInnerMostDims("all",t,e.rank);var n=computeOutAndReduceShapes(e.shape,t),r=n[0],a=sizeFromShape(n[1]),o=e.as2D(-1,a);return this.reduce(o,"all",o.dtype).reshape(r)},e.prototype.squaredDifference=function(e,t){var n=new BinaryOpProgram(SQUARED_DIFFERENCE,e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.realDivide=function(e,t){var n=new BinaryOpProgram(DIV,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"float32");return this.compileAndRun(n,[e,t],r)},e.prototype.floorDiv=function(e,t){var n=new BinaryOpProgram(INT_DIV,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"int32");return this.compileAndRun(n,[e,t],r)},e.prototype.add=function(e,t){var n=new BinaryOpProgram(ADD,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,upcastType(e.dtype,t.dtype));return this.compileAndRun(n,[e,t],r)},e.prototype.subtract=function(e,t){var n=new BinaryOpProgram(SUB,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,upcastType(e.dtype,t.dtype));return this.compileAndRun(n,[e,t],r)},e.prototype.pow=function(e,t){var n=new BinaryOpProgram(POW,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,upcastType(e.dtype,t.dtype));return this.compileAndRun(n,[e,t],r)},e.prototype.ceil=function(e){var t=new UnaryOpProgram(e.shape,CEIL);return this.compileAndRun(t,[e])},e.prototype.floor=function(e){var t=new UnaryOpProgram(e.shape,FLOOR);return this.compileAndRun(t,[e])},e.prototype.sign=function(e){var t=new UnaryOpProgram(e.shape,SIGN);return this.compileAndRun(t,[e])},e.prototype.round=function(e){var t=new UnaryOpProgram(e.shape,ROUND);return this.compileAndRun(t,[e])},e.prototype.exp=function(e){var t=new UnaryOpProgram(e.shape,EXP);return this.compileAndRun(t,[e])},e.prototype.expm1=function(e){var t=new UnaryOpProgram(e.shape,EXPM1);return this.compileAndRun(t,[e])},e.prototype.log=function(e){var t=new UnaryOpProgram(e.shape,LOG);return this.compileAndRun(t,[e])},e.prototype.log1p=function(e){var t=new UnaryOpProgram(e.shape,LOG1P);return this.compileAndRun(t,[e])},e.prototype.sqrt=function(e){var t=new UnaryOpProgram(e.shape,SQRT);return this.compileAndRun(t,[e])},e.prototype.rsqrt=function(e){var t=new UnaryOpProgram(e.shape,RSQRT);return this.compileAndRun(t,[e])},e.prototype.square=function(e){var t=new UnaryOpProgram(e.shape,SQUARE);return this.compileAndRun(t,[e])},e.prototype.reciprocal=function(e){var t=new UnaryOpProgram(e.shape,RECIPROCAL);return this.compileAndRun(t,[e])},e.prototype.relu=function(e){var t=new UnaryOpProgram(e.shape,RELU);return this.compileAndRun(t,[e])},e.prototype.elu=function(e){var t=new UnaryOpProgram(e.shape,ELU);return this.compileAndRun(t,[e])},e.prototype.eluDer=function(e,t){var n=new BinaryOpProgram(ELU_DER,e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.selu=function(e){var t=new UnaryOpProgram(e.shape,SELU);return this.compileAndRun(t,[e])},e.prototype.int=function(e){var t=new UnaryOpProgram(e.shape,TO_INT),n=this.makeOutputArray(t.outputShape,"int32");return this.compileAndRun(t,[e],n)},e.prototype.clip=function(e,t,n){var r=new ClipProgram(e.shape,t,n);return this.compileAndRun(r,[e])},e.prototype.abs=function(e){var t=new UnaryOpProgram(e.shape,ABS);return this.compileAndRun(t,[e])},e.prototype.sigmoid=function(e){var t=new UnaryOpProgram(e.shape,SIGMOID);return this.compileAndRun(t,[e])},e.prototype.softplus=function(e){var t=new UnaryOpProgram(e.shape,SOFTPLUS);return this.compileAndRun(t,[e])},e.prototype.sin=function(e){var t=new UnaryOpProgram(e.shape,SIN);return this.compileAndRun(t,[e])},e.prototype.cos=function(e){var t=new UnaryOpProgram(e.shape,COS);return this.compileAndRun(t,[e])},e.prototype.tan=function(e){var t=new UnaryOpProgram(e.shape,TAN);return this.compileAndRun(t,[e])},e.prototype.asin=function(e){var t=new UnaryOpProgram(e.shape,ASIN);return this.compileAndRun(t,[e])},e.prototype.acos=function(e){var t=new UnaryOpProgram(e.shape,ACOS);return this.compileAndRun(t,[e])},e.prototype.atan=function(e){var t=new UnaryOpProgram(e.shape,ATAN);return this.compileAndRun(t,[e])},e.prototype.atan2=function(e,t){var n=new BinaryOpProgram(ATAN2,e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.sinh=function(e){var t=new UnaryOpProgram(e.shape,SINH);return this.compileAndRun(t,[e])},e.prototype.cosh=function(e){var t=new UnaryOpProgram(e.shape,COSH);return this.compileAndRun(t,[e])},e.prototype.tanh=function(e){var t=new UnaryOpProgram(e.shape,TANH);return this.compileAndRun(t,[e])},e.prototype.asinh=function(e){var t=new UnaryOpProgram(e.shape,ASINH);return this.compileAndRun(t,[e])},e.prototype.acosh=function(e){var t=new UnaryOpProgram(e.shape,ACOSH);return this.compileAndRun(t,[e])},e.prototype.atanh=function(e){var t=new UnaryOpProgram(e.shape,ATANH);return this.compileAndRun(t,[e])},e.prototype.erf=function(e){var t=new UnaryOpProgram(e.shape,ERF);return this.compileAndRun(t,[e])},e.prototype.step=function(e,t){var n=new UnaryOpProgram(e.shape,STEP(t));return this.compileAndRun(n,[e])},e.prototype.conv2d=function(e,t,n){var r=new Conv2DProgram(n);return this.compileAndRun(r,[e,t])},e.prototype.conv2dDerInput=function(e,t,n){var r=new Conv2DDerInputProgram(n);return this.compileAndRun(r,[e,t])},e.prototype.conv2dDerFilter=function(e,t,n){var r=new Conv2DDerFilterProgram(n);return this.compileAndRun(r,[e,t])},e.prototype.depthwiseConv2D=function(e,t,n){var r=new DepthwiseConv2DProgram(n);return this.compileAndRun(r,[e,t])},e.prototype.depthwiseConv2DDerInput=function(e,t,n){var r=new DepthwiseConv2DDerInputProgram(n);return this.compileAndRun(r,[e,t])},e.prototype.depthwiseConv2DDerFilter=function(e,t,n){var r=new DepthwiseConv2DDerFilterProgram(n);return this.compileAndRun(r,[e,t])},e.prototype.maxPool=function(e,t){var n=new Pool2DProgram(t,"max",!1),r=this.makeOutputArray(n.outputShape,e.dtype);return this.compileAndRun(n,[e],r)},e.prototype.avgPool=function(e,t){var n=new Pool2DProgram(t,"avg",!1),r=this.makeOutputArray(n.outputShape,"float32");return this.compileAndRun(n,[e],r)},e.prototype.maxPoolBackprop=function(e,t,n,r){var a=new Pool2DProgram(r,"max",!0),o=this.compileAndRun(a,[t]),i=new MaxPool2DBackpropProgram(r),s=this.makeOutputArray(i.outputShape,t.dtype),u=this.compileAndRun(i,[e,o],s);return o.dispose(),u},e.prototype.avgPoolBackprop=function(e,t,n){var r=new AvgPool2DBackpropProgram(n),a=this.makeOutputArray(r.outputShape,t.dtype);return this.compileAndRun(r,[e],a)},e.prototype.cast=function(e,t){return castTensor(e,t,this)},e.prototype.reshape=function(e,t){return reshapeTensor(e,t)},e.prototype.resizeBilinear=function(e,t,n,r){var a=new ResizeBilinearProgram(e.shape,t,n,r);return this.compileAndRun(a,[e])},e.prototype.resizeBilinearBackprop=function(e,t,n){var r=new ResizeBilinearBackpropProgram(e,t,n);return this.compileAndRun(r,[e])},e.prototype.resizeNearestNeighbor=function(e,t,n,r){var a=new ResizeNearestNeighborProgram(e.shape,t,n,r);return this.compileAndRun(a,[e])},e.prototype.multinomial=function(e,t,n,r){var a=t?e:softmax(e),o=a.shape[0],i=a.shape[1],s=new MultinomialProgram(o,i,n),u=this.makeOutputArray(s.outputShape,"int32"),l=s.getCustomSetupFunc(r);return this.compileAndRun(s,[a],u,l)},e.prototype.oneHot=function(e,t,n,r){var a=new OneHotProgram(e.size,t,n,r);return this.compileAndRun(a,[e])},e.prototype.makeOutputArray=function(e,t){return Tensor.make(e,{},t)},e.prototype.compileAndRun=function(e,t,n,r){var a=this;null==n&&(n=this.makeOutputArray(e.outputShape,t[0].dtype));var o=t.map(function(e){var t=a.texData.get(e.dataId);return null==t.texture&&e.size<=SIZE_UPLOAD_UNIFORM?{tensor:e,texData:null,isUniform:!0}:(a.uploadToGPU(e.dataId),{tensor:e,texData:t,isUniform:!1})});this.uploadToGPU(n.dataId);var i,s={tensor:n,texData:this.texData.get(n.dataId),isUniform:!1},u=makeShaderKey(e,o,s),l=this.getAndSaveBinary(u,function(){return compileProgram(a.gpgpu,e,o,s)}),c=null!=this.activeTimers;if(c&&(i=this.startTimer()),runProgram(l,o,s,r),this.numBytesInGPU>this.NUM_BYTES_BEFORE_PAGING)for(var d=this.numBytesInGPU-this.NUM_BYTES_BEFORE_PAGING;d>0;){var p=this.lruDataGPU.shift(),h=this.texData.get(p),f=h.shape,g=h.dtype;d-=this.computeBytes(f,g),this.read(p);}return c&&(i=this.endTimer(i),this.activeTimers.push(this.getQueryTime(i))),n},e.prototype.getAndSaveBinary=function(e,t){return e in this.binaryCache||(this.binaryCache[e]=t()),this.binaryCache[e]},e.prototype.getTextureManager=function(){return this.textureManager},e.prototype.dispose=function(){if(!this.disposed){for(var e in this.binaryCache)this.gpgpu.deleteProgram(this.binaryCache[e].webGLProgram);this.textureManager.dispose(),this.canvas.remove(),null!=this.fromPixelsCanvas&&this.fromPixelsCanvas.remove(),this.gpgpuCreatedLocally&&this.gpgpu.dispose(),this.disposed=!0;}},e.prototype.throwIfNoData=function(e){if(!this.texData.has(e))throw new Error("WebGL backend: No data found for this tensor. Did you change your backend in the middle of the program? New backends can't use Tensors created with previous backends")},e.prototype.uploadToGPU=function(e){this.throwIfNoData(e);var t=this.texData.get(e),n=t.shape,r=t.values,a=t.texture,o=t.dtype,i=t.usage;if(null!=a)return this.lruDataGPU.splice(this.lruDataGPU.indexOf(e),1),void this.lruDataGPU.push(e);var s,u=null!=this.activeTimers;u&&(s=performance.now());var l=getTextureShapeFromLogicalShape(this.gpgpu.gl,n);t.texShape=l;var c=this.acquireTexture(e,l,i);t.texture=c,null!=r&&(this.gpgpu.uploadMatrixToTexture(c,l[0],l[1],typedArrayToFloat32(r,o)),t.values=null,u&&(this.uploadWaitMs+=performance.now()-s));},e.prototype.cacheOnCPU=function(e,t){var n=this.delayedStorage,r=this.texData.get(e),a=r.texture,o=r.texShape,i=r.dtype,s=r.usage;n&&null!=a&&(this.releaseTexture(e,a,o,s),r.texture=null,r.texShape=null),null!=t&&(r.values=float32ToTypedArray(t,i));},e.prototype.releaseTexture=function(e,t,n,r){var a=this.texData.get(e),o=a.shape,i=a.dtype,s=this.lruDataGPU.indexOf(e);s>=0&&this.lruDataGPU.splice(s,1),this.numBytesInGPU-=this.computeBytes(o,i),this.textureManager.releaseTexture(t,n,r);},e.prototype.acquireTexture=function(e,t,n){var r=this.texData.get(e),a=r.shape,o=r.dtype;return this.lruDataGPU.push(e),this.numBytesInGPU+=this.computeBytes(a,o),this.textureManager.acquireTexture(t,n)},e.prototype.computeBytes=function(e,t){return sizeFromShape(e)*bytesPerElement(t)},e}();function float32ToTypedArray(e,t){if("float32"===t)return e;if("int32"===t||"bool"===t){for(var n="int32"===t?new Int32Array(e.length):new Uint8Array(e.length),r=0;rp&&(p=g,h=f);}u[c]=h;}return i},e.prototype.cumsum=function(e,t,n,r){if(t!==e.rank-1)throw new Error("backend.cumsum in CPU expects an inner-most axis="+(e.rank-1)+" but got axis="+t);for(var a=upcastType(e.dtype,"int32"),o=zeros(e.shape,a),i=o.dataSync(),s=e.dataSync(),u=e.shape[e.rank-1],l=r?function(e,t){return e+u-t-1}:function(e,t){return e+t},c=0;ct?1:0})},e.prototype.greaterEqual=function(e,t){return this.broadcastedBinaryOp(e,t,"bool",function(e,t){return e>=t?1:0})},e.prototype.logicalNot=function(e){for(var t=e.dataSync(),n=new Int32Array(t.length),r=0;r1||1===t.rank?1:t.shape[1],d=0;d=0&&t>=0?n:(n+t)%t})},e.prototype.max=function(e,t){assertAxesAreInnerMostDims("max",t,e.rank);for(var n=computeOutAndReduceShapes(e.shape,t),r=n[0],a=n[1],o=zeros(r,e.dtype),i=sizeFromShape(a),s=o.dataSync(),u=e.dataSync(),l=0;ld&&(d=h);}s[l]=d;}return o},e.prototype.maximum=function(e,t){return this.broadcastedBinaryOp(e,t,e.dtype,function(e,t){return Math.max(e,t)})},e.prototype.all=function(e,t){assertAxesAreInnerMostDims("all",t,e.rank);for(var n=computeOutAndReduceShapes(e.shape,t),r=n[0],a=n[1],o=zeros(r,e.dtype),i=sizeFromShape(a),s=o.dataSync(),u=e.dataSync(),l=0;l0?n[r]=1:n[r]=0;return Tensor.make(e.shape,{values:n})},e.prototype.round=function(e){for(var t=e.dataSync(),n=new Float32Array(t.length),r=0;r.5?n[r]=Math.ceil(t[r]):n[r]=a%2==0?a:a+1;}return Tensor.make(e.shape,{values:n})},e.prototype.exp=function(e){for(var t=e.dataSync(),n=new Float32Array(t.length),r=0;r=0?a:Math.exp(a)-1;}return Tensor.make(e.shape,{values:t})},e.prototype.eluDer=function(e,t){for(var n=new Float32Array(t.size),r=t.dataSync(),a=e.dataSync(),o=0;o=1?a[o]:a[o]*(i+1);}return Tensor.make(t.shape,{values:n})},e.prototype.selu=function(e){for(var t=SELU_SCALEALPHA,n=SELU_SCALE,r=new Float32Array(e.size),a=e.dataSync(),o=0;o=0?n*i:t*(Math.exp(i)-1);}return Tensor.make(e.shape,{values:r})},e.prototype.clip=function(e,t,n){for(var r=new Float32Array(e.size),a=e.dataSync(),o=0;o-t,i=r[a]0?1:t;}return Tensor.make(e.shape,{values:n})},e.prototype.conv2d=function(e,t,n){for(var r=n.filterHeight,a=n.filterWidth,o=n.dilationHeight,i=n.dilationWidth,s=n.padInfo.left,u=n.padInfo.top,l=buffer(n.outShape,e.dtype),c=0;c=n.inHeight))for(var x=0;x=n.inWidth))for(var E=0;E=n.inHeight))for(var E=0;E=n.inWidth))y+=e.get(d,b,S,p)*t.get(x,E,p,v);}}c.set(y,d,h,g,p*l+v);}return c.toTensor()},e.prototype.depthwiseConv2DDerInput=function(e,t,n){for(var r=buffer(n.inShape,"float32"),a=r.values,o=r.strides,i=o[0],s=o[1],u=o[2],l=e.dataSync(),c=e.strides,d=c[0],p=c[1],h=c[2],f=t.dataSync(),g=t.strides,m=g[0],v=g[1],y=g[2],x=n.batchSize,b=n.filterHeight,E=n.filterWidth,S=n.inChannels,A=n.inHeight,T=n.inWidth,w=n.outChannels,_=n.outHeight,R=n.outWidth,C=n.strideHeight,O=n.strideWidth,I=b-1-n.padInfo.top,k=E-1-n.padInfo.left,M=w/S,N=0;Nb?b=w:"avg"===n&&(E+=w,S++);}if(isNaN(b))break}s.set("avg"===n?E/S:b,c,p,m,d);}return s.toTensor()},e.prototype.maxPool=function(e,t){return this.pool(e,t,"max")},e.prototype.maxPoolPositions=function(e,t){for(var n=buffer(t.outShape,"int32"),r=t.strideHeight,a=t.strideWidth,o=t.filterHeight,i=t.filterWidth,s=t.padInfo.top,u=t.padInfo.left,l=0;lx&&(x=w,b=S*i+T);}n.set(b,l,d,g,c);}return n.toTensor()},e.prototype.maxPoolBackprop=function(e,t,n,r){for(var a=this.maxPoolPositions(t,r),o=r.strideHeight,i=r.strideWidth,s=r.filterHeight,u=r.filterWidth,l=u-1-r.padInfo.left,c=s-1-r.padInfo.top,d=buffer(t.shape,"float32"),p=0;p=r.outHeight||Math.floor(b)!==b))for(var E=0;E=r.outWidth||Math.floor(S)!==S)){var A=s*u-1-a.get(p,b,S,h)===x*u+E?1:0;if(0!==A)y+=e.get(p,b,S,h)*A;}}}d.set(y,p,f,g,h);}return d.toTensor()},e.prototype.avgPoolBackprop=function(e,t,n){for(var r=n.strideHeight,a=n.strideWidth,o=n.filterHeight,i=n.filterWidth,s=i-1-n.padInfo.left,u=o-1-n.padInfo.top,l=buffer(t.shape,"float32"),c=1/(o*i),d=0;d=n.outHeight||Math.floor(x)!==x))for(var b=0;b=n.outWidth||Math.floor(E)!==E))v+=e.get(d,x,E,p);}}l.set(v*c,d,h,f,p);}return l.toTensor()},e.prototype.cast=function(e,t){return castTensor(e,t,this)},e.prototype.reshape=function(e,t){return reshapeTensor(e,t)},e.prototype.avgPool=function(e,t){return this.pool(e,t,"avg").toFloat()},e.prototype.resizeBilinear=function(e,t,n,r){for(var a=e.shape,o=a[0],i=a[1],s=a[2],u=a[3],l=buffer([o,t,n,u],e.dtype),c=[r&&t>1?i-1:i,r&&n>1?s-1:s],d=[r&&t>1?t-1:t,r&&n>1?n-1:n],p=0;p1?o-1:o,n&&c>1?i-1:i],h=[n&&l>1?l-1:l,n&&c>1?c-1:c],f=p[0]/h[0],g=p[1]/h[1],m=0;m=0&&e.get(o)0,"scheme must not be an empty string.");var r=e.getInstance();assert(null==r.managers[t],"A model store manager is already registered for scheme '"+t+"'."),r.managers[t]=n;},e.getManager=function(e){var t=this.getInstance().managers[e];if(null==t)throw new Error("Cannot find model manager for scheme '"+e+"'");return t},e.getSchemes=function(){return Object.keys(this.getInstance().managers)},e}();function parseURL(e){if(-1===e.indexOf(URL_SCHEME_SUFFIX))throw new Error("The url string provided does not contain a scheme. Supported schemes are: "+ModelStoreManagerRegistry.getSchemes().join(","));return {scheme:e.split(URL_SCHEME_SUFFIX)[0],path:e.split(URL_SCHEME_SUFFIX)[1]}}function cloneModelInternal(e,t,n){return void 0===n&&(n=!1),__awaiter(this,void 0,void 0,function(){var r,a,o,i,s,u,l,c,d;return __generator(this,function(p){switch(p.label){case 0:return assert(e!==t,"Old path and new path are the same: '"+e+"'"),assert((r=IORouterRegistry.getLoadHandlers(e)).length>0,"Copying failed because no load handler is found for source URL "+e+"."),assert(r.length<2,"Copying failed because more than one ("+r.length+") load handlers for source URL "+e+"."),a=r[0],assert((o=IORouterRegistry.getSaveHandlers(t)).length>0,"Copying failed because no save handler is found for destination URL "+t+"."),assert(o.length<2,"Copying failed because more than one ("+r.length+") save handlers for destination URL "+t+"."),i=o[0],s=parseURL(e).scheme,u=parseURL(e).path,l=s===parseURL(e).scheme,[4,a.load()];case 1:return c=p.sent(),n&&l?[4,ModelStoreManagerRegistry.getManager(s).removeModel(u)]:[3,3];case 2:p.sent(),p.label=3;case 3:return [4,i.save(c)];case 4:return d=p.sent(),!n||l?[3,6]:[4,ModelStoreManagerRegistry.getManager(s).removeModel(u)];case 5:p.sent(),p.label=6;case 6:return [2,d.modelArtifactsInfo]}})})}var ModelManagement=function(){function e(){}return e.listModels=function(){return __awaiter(this,void 0,void 0,function(){var e,t,n,r,a,o,i;return __generator(this,function(s){switch(s.label){case 0:e=ModelStoreManagerRegistry.getSchemes(),t={},n=0,r=e,s.label=1;case 1:return n0,"URL path for browserHTTPRequest must not be null, undefined or empty."),this.path=e,null!=t&&null!=t.body)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=t||{};}return e.prototype.save=function(e){return __awaiter(this,void 0,void 0,function(){var t,n,r,a;return __generator(this,function(o){switch(o.label){case 0:if(e.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");return (t=Object.assign({method:this.DEFAULT_METHOD},this.requestInit)).body=new FormData,n=[{paths:["./model.weights.bin"],weights:e.weightSpecs}],r={modelTopology:e.modelTopology,weightsManifest:n},t.body.append("model.json",new Blob([JSON.stringify(r)],{type:"application/json"}),"model.json"),null!=e.weightData&&t.body.append("model.weights.bin",new Blob([e.weightData],{type:"application/octet-stream"}),"model.weights.bin"),[4,fetch(this.path,t)];case 1:if(200===(a=o.sent()).status)return [2,{modelArtifactsInfo:getModelArtifactsInfoForJSON(e),responses:[a]}];throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status "+a.status+".")}})})},e.prototype.load=function(){return __awaiter(this,void 0,void 0,function(){var e,t,n,r,a,o,i,s,u,l,c,d;return __generator(this,function(p){switch(p.label){case 0:return [4,fetch(this.path,this.requestInit)];case 1:return [4,p.sent().json()];case 2:if(e=p.sent(),t=e.modelTopology,n=e.weightsManifest,null==t&&null==n)throw new Error("The JSON from HTTP path "+this.path+" contains neither model topology or manifest for weights.");if(null==n)return [3,4];for(o=e.weightsManifest,r=[],i=0,s=o;in)}function expectValuesInRange(e,t,n){var r;r=e instanceof Tensor?e.dataSync():e;for(var a=0;an)throw new Error("Value out of range:"+r[a]+" low: "+t+", high: "+n)}function expectArrayBuffersEqual(e,t){expect(new Float32Array(e)).toEqual(new Float32Array(t));}var test_util=Object.freeze({WEBGL_ENVS:WEBGL_ENVS,CPU_ENVS:CPU_ENVS,CHROME_CPU_ENVS:CHROME_CPU_ENVS,NATIVE_ENV:NATIVE_ENV,BROWSER_ENVS:BROWSER_ENVS,ALL_ENVS:ALL_ENVS,expectArraysClose:expectArraysClose,expectPromiseToFail:expectPromiseToFail,expectArraysEqual:expectArraysEqual,expectNumbersClose:expectNumbersClose,expectValuesInRange:expectValuesInRange,expectArrayBuffersEqual:expectArrayBuffersEqual}),version="0.11.9",webgl=Object.freeze({gpgpu_util:gpgpu_util,webgl_util:webgl_util,MathBackendWebGL:MathBackendWebGL,GPGPUContext:GPGPUContext}),Optimizer=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return __extends(t,e),t.prototype.minimize=function(e,t,n){void 0===t&&(t=!1);var r=this.computeGradients(e,n),a=r.value,o=r.grads;return this.applyGradients(o),Object.keys(o).forEach(function(e){return o[e].dispose()}),t?a:(a.dispose(),null)},t.prototype.computeGradients=function(e,t){return variableGrads(e,t)},__decorate([doc({heading:"Training",subheading:"Optimizers"})],t.prototype,"minimize",null),t=__decorate([doc({heading:"Training",subheading:"Classes",namespace:"train"})],t)}(Serializable),DEFAULT_FLOAT32_EPSILON=1e-8,DEFAULT_FLOAT16_EPSILON=1e-4;function getOptimizerDefaultEpsilonValue(){return ENV.get("WEBGL_RENDER_FLOAT32_ENABLED")?DEFAULT_FLOAT32_EPSILON:DEFAULT_FLOAT16_EPSILON}var AdadeltaOptimizer=function(e){function t(t,n,r){void 0===r&&(r=null);var a=e.call(this)||this;return a.learningRate=t,a.rho=n,a.epsilon=r,a.accumulatedGrads={},a.accumulatedUpdates={},a.c=keep(scalar(-t)),a.rhoScalar=keep(scalar(n)),a.oneMinusRho=keep(scalar(1-n)),null===r&&(r=getOptimizerDefaultEpsilonValue()),a.epsilonScalar=keep(scalar(r)),a}return __extends(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=ENV.engine.registeredVariables[n];if(null==r.accumulatedGrads[n]){tidy(function(){t.accumulatedGrads[n]=zerosLike(a).variable(!1);});}if(null==r.accumulatedUpdates[n]){tidy(function(){t.accumulatedUpdates[n]=zerosLike(a).variable(!1);});}var o=e[n],i=r.accumulatedGrads[n],s=r.accumulatedUpdates[n];tidy(function(){var e=t.rhoScalar.mul(i).add(t.oneMinusRho.mul(o.square())),r=s.add(t.epsilonScalar).sqrt().div(i.add(t.epsilonScalar).sqrt()).mul(o),u=t.rhoScalar.mul(s).add(t.oneMinusRho.mul(r.square()));t.accumulatedGrads[n].assign(e),t.accumulatedUpdates[n].assign(u);var l=t.c.mul(r).add(a);a.assign(l);});},r=this;for(var a in e)n(a);},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsilonScalar.dispose(),this.rhoScalar.dispose(),this.oneMinusRho.dispose(),null!=this.accumulatedUpdates&&(Object.keys(this.accumulatedUpdates).forEach(function(t){return e.accumulatedUpdates[t].dispose()}),Object.keys(this.accumulatedGrads).forEach(function(t){return e.accumulatedGrads[t].dispose()}));},t.prototype.getConfig=function(){return {learningRate:this.learningRate,rho:this.rho,epsilon:this.epsilon}},t.fromConfig=function(e,t){return new e(t.learningRate,t.rho,t.epsilon)},t.className="AdadeltaOptimizer",t}(Optimizer);SerializationMap.register(AdadeltaOptimizer);var AdagradOptimizer=function(e){function t(t,n){void 0===n&&(n=.1);var r=e.call(this)||this;r.learningRate=t,r.initialAccumulatorValue=n,r.accumulatedGrads={},r.c=keep(scalar(-t));var a=getOptimizerDefaultEpsilonValue();return r.epsilon=keep(scalar(a)),r}return __extends(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=ENV.engine.registeredVariables[n];if(null==r.accumulatedGrads[n]){tidy(function(){t.accumulatedGrads[n]=fill(a.shape,t.initialAccumulatorValue).variable(!1);});}var o=e[n],i=r.accumulatedGrads[n];tidy(function(){var e=i.add(o.square());t.accumulatedGrads[n].assign(e);var r=t.c.mul(o.div(e.add(t.epsilon).sqrt())).add(a);a.assign(r);});},r=this;for(var a in e)n(a);},t.prototype.dispose=function(){var e=this;this.epsilon.dispose(),this.c.dispose(),null!=this.accumulatedGrads&&Object.keys(this.accumulatedGrads).forEach(function(t){return e.accumulatedGrads[t].dispose()});},t.prototype.getConfig=function(){return {learningRate:this.learningRate,initialAccumulatorValue:this.initialAccumulatorValue}},t.fromConfig=function(e,t){return new e(t.learningRate,t.initialAccumulatorValue)},t.className="AdagradOptimizer",t}(Optimizer);SerializationMap.register(AdagradOptimizer);var AdamOptimizer=function(e){function t(t,n,r,a){void 0===a&&(a=null);var o=e.call(this)||this;return o.learningRate=t,o.beta1=n,o.beta2=r,o.epsilon=a,o.accumulatedFirstMoment={},o.accumulatedSecondMoment={},o.c=keep(scalar(-t)),o.beta1Scalar=keep(scalar(n)),o.beta2Scalar=keep(scalar(r)),tidy(function(){o.accBeta1=scalar(n).variable(),o.accBeta2=scalar(r).variable();}),o.oneMinusBeta1=keep(scalar(1-n)),o.oneMinusBeta2=keep(scalar(1-r)),o.one=keep(scalar(1)),null===a&&(a=getOptimizerDefaultEpsilonValue()),o.epsScalar=keep(scalar(a)),o}return __extends(t,e),t.prototype.applyGradients=function(e){var t=this;tidy(function(){var n=t.one.sub(t.accBeta1),r=t.one.sub(t.accBeta2);for(var a in e){var o=ENV.engine.registeredVariables[a];if(null==t.accumulatedFirstMoment[a]){var i=!1;t.accumulatedFirstMoment[a]=zerosLike(o).variable(i);}if(null==t.accumulatedSecondMoment[a]){i=!1;t.accumulatedSecondMoment[a]=zerosLike(o).variable(i);}var s=e[a],u=t.accumulatedFirstMoment[a],l=t.accumulatedSecondMoment[a],c=t.beta1Scalar.mul(u).add(t.oneMinusBeta1.mul(s)),d=t.beta2Scalar.mul(l).add(t.oneMinusBeta2.mul(s.square())),p=c.div(n),h=d.div(r);t.accumulatedFirstMoment[a].assign(c),t.accumulatedSecondMoment[a].assign(d);var f=t.c.mul(p.div(t.epsScalar.add(h.sqrt()))).add(o);o.assign(f);}t.accBeta1.assign(t.accBeta1.mul(t.beta1Scalar)),t.accBeta2.assign(t.accBeta2.mul(t.beta2Scalar));});},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsScalar.dispose(),this.beta1Scalar.dispose(),this.beta2Scalar.dispose(),this.accBeta1.dispose(),this.accBeta2.dispose(),this.oneMinusBeta1.dispose(),this.oneMinusBeta2.dispose(),this.one.dispose(),null!=this.accumulatedFirstMoment&&Object.keys(this.accumulatedFirstMoment).forEach(function(t){return e.accumulatedFirstMoment[t].dispose()}),null!=this.accumulatedSecondMoment&&Object.keys(this.accumulatedSecondMoment).forEach(function(t){return e.accumulatedSecondMoment[t].dispose()});},t.prototype.getConfig=function(){return {learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon}},t.fromConfig=function(e,t){return new e(t.learningRate,t.beta1,t.beta2,t.epsilon)},t.className="AdamOptimizer",t}(Optimizer);SerializationMap.register(AdamOptimizer);var AdamaxOptimizer=function(e){function t(t,n,r,a,o){void 0===a&&(a=null),void 0===o&&(o=0);var i=e.call(this)||this;return i.learningRate=t,i.beta1=n,i.beta2=r,i.epsilon=a,i.decay=o,i.accumulatedFirstMoment={},i.accumulatedWeightedInfNorm={},i.c=keep(scalar(-t)),i.beta1Scalar=keep(scalar(n)),i.beta2Scalar=keep(scalar(r)),i.decayScalar=keep(scalar(o)),tidy(function(){i.iteration=scalar(0).variable(),i.accBeta1=scalar(n).variable();}),i.oneMinusBeta1=keep(scalar(1-n)),i.one=keep(scalar(1)),null===a&&(a=getOptimizerDefaultEpsilonValue()),i.epsScalar=keep(scalar(a)),i}return __extends(t,e),t.prototype.applyGradients=function(e){var t=this;tidy(function(){var n=t.one.sub(t.accBeta1),r=t.c.div(t.one.add(t.decayScalar.mul(t.iteration)));for(var a in e){var o=ENV.engine.registeredVariables[a];if(null==t.accumulatedFirstMoment[a]){var i=!1;t.accumulatedFirstMoment[a]=zerosLike(o).variable(i);}if(null==t.accumulatedWeightedInfNorm[a]){i=!1;t.accumulatedWeightedInfNorm[a]=zerosLike(o).variable(i);}var s=e[a],u=t.accumulatedFirstMoment[a],l=t.accumulatedWeightedInfNorm[a],c=t.beta1Scalar.mul(u).add(t.oneMinusBeta1.mul(s)),d=t.beta2Scalar.mul(l),p=s.abs(),h=d.maximum(p);t.accumulatedFirstMoment[a].assign(c),t.accumulatedWeightedInfNorm[a].assign(h);var f=r.div(n).mul(c.div(t.epsScalar.add(h))).add(o);o.assign(f);}t.iteration.assign(t.iteration.add(t.one)),t.accBeta1.assign(t.accBeta1.mul(t.beta1Scalar));});},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsScalar.dispose(),this.accBeta1.dispose(),this.beta1Scalar.dispose(),this.beta2Scalar.dispose(),this.oneMinusBeta1.dispose(),this.decayScalar.dispose(),this.iteration.dispose(),this.one.dispose(),null!=this.accumulatedFirstMoment&&Object.keys(this.accumulatedFirstMoment).forEach(function(t){return e.accumulatedFirstMoment[t].dispose()}),null!=this.accumulatedWeightedInfNorm&&Object.keys(this.accumulatedWeightedInfNorm).forEach(function(t){return e.accumulatedWeightedInfNorm[t].dispose()});},t.prototype.getConfig=function(){return {learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon,decay:this.decay}},t.fromConfig=function(e,t){return new e(t.learningRate,t.beta1,t.beta2,t.epsilon,t.decay)},t.className="AdamaxOptimizer",t}(Optimizer);SerializationMap.register(AdamaxOptimizer);var SGDOptimizer=function(e){function t(t){var n=e.call(this)||this;return n.learningRate=t,n.setLearningRate(t),n}return __extends(t,e),t.prototype.applyGradients=function(e){var t=this;Object.keys(e).forEach(function(n){var r=e[n],a=ENV.engine.registeredVariables[n];tidy(function(){var e=t.c.mul(r).add(a);a.assign(e);});});},t.prototype.setLearningRate=function(e){this.learningRate=e,null!=this.c&&this.c.dispose(),this.c=keep(scalar(-e));},t.prototype.dispose=function(){this.c.dispose();},t.prototype.getConfig=function(){return {learningRate:this.learningRate}},t.fromConfig=function(e,t){return new e(t.learningRate)},t.className="SGDOptimizer",t}(Optimizer);SerializationMap.register(SGDOptimizer);var MomentumOptimizer=function(e){function t(t,n,r){void 0===r&&(r=!1);var a=e.call(this,t)||this;return a.learningRate=t,a.momentum=n,a.useNesterov=r,a.m=scalar(a.momentum),a.accumulations={},a}return __extends(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=ENV.engine.registeredVariables[n];if(null==r.accumulations[n]){tidy(function(){t.accumulations[n]=zerosLike(a).variable(!1);});}var o=r.accumulations[n],i=e[n];tidy(function(){var e,r=t.m.mul(o).add(i);e=t.useNesterov?t.c.mul(i.add(r.mul(t.m))).add(a):t.c.mul(r).add(a),t.accumulations[n].assign(r),a.assign(e);});},r=this;for(var a in e)n(a);},t.prototype.dispose=function(){if(e.prototype.dispose.call(this),this.m.dispose(),null!=this.accumulations)for(var t in this.accumulations)this.accumulations[t].dispose();},t.prototype.setMomentum=function(e){this.momentum=e;},t.prototype.getConfig=function(){return {learningRate:this.learningRate,momentum:this.momentum,useNesterov:this.useNesterov}},t.fromConfig=function(e,t){return new e(t.learningRate,t.momentum,t.useNesterov)},t.className="MomentumOptimizer",t}(SGDOptimizer);SerializationMap.register(MomentumOptimizer);var RMSPropOptimizer=function(e){function t(t,n,r,a,o){void 0===n&&(n=.9),void 0===r&&(r=0),void 0===a&&(a=null),void 0===o&&(o=!1);var i=e.call(this)||this;return i.learningRate=t,i.decay=n,i.momentum=r,i.epsilon=a,i.accumulatedMeanSquares={},i.accumulatedMeanGrads={},i.accumulatedMoments={},i.c=keep(scalar(t)),i.decayScalar=keep(scalar(n)),i.momentumScalar=keep(scalar(r)),i.oneMinusDecay=keep(scalar(1-n)),i.centered=o,null===a&&(a=getOptimizerDefaultEpsilonValue()),i.epsilonScalar=keep(scalar(a)),i}return __extends(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=ENV.engine.registeredVariables[n];if(null==r.accumulatedMeanSquares[n]){tidy(function(){t.accumulatedMeanSquares[n]=zerosLike(a).variable(!1);});}if(null==r.accumulatedMeanGrads[n]&&r.centered){tidy(function(){t.accumulatedMeanGrads[n]=zerosLike(a).variable(!1);});}if(null==r.accumulatedMoments[n]){tidy(function(){t.accumulatedMoments[n]=zerosLike(a).variable(!1);});}var o=r.accumulatedMeanSquares[n],i=r.accumulatedMeanGrads[n],s=r.accumulatedMoments[n],u=e[n];tidy(function(){var e=t.decayScalar.mul(o).add(t.oneMinusDecay.mul(u.square()));if(t.centered){var r=t.decayScalar.mul(i).add(t.oneMinusDecay.mul(u)),l=t.momentumScalar.mul(s).add(t.c.mul(u).div(e.sub(r.square().add(t.epsilonScalar)).sqrt()));t.accumulatedMeanSquares[n].assign(e),t.accumulatedMeanGrads[n].assign(r),t.accumulatedMoments[n].assign(l);var c=a.sub(l);a.assign(c);}else{var d=t.decayScalar.mul(o).add(t.oneMinusDecay.mul(u.square()));l=t.momentumScalar.mul(s).add(t.c.mul(u).div(d.add(t.epsilonScalar).sqrt()));t.accumulatedMeanSquares[n].assign(d),t.accumulatedMoments[n].assign(l);c=a.sub(l);a.assign(c);}});},r=this;for(var a in e)n(a);},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsilonScalar.dispose(),this.decayScalar.dispose(),this.momentumScalar.dispose(),this.oneMinusDecay.dispose(),null!=this.accumulatedMeanSquares&&Object.keys(this.accumulatedMeanSquares).forEach(function(t){return e.accumulatedMeanSquares[t].dispose()}),null!=this.accumulatedMeanGrads&&this.centered&&Object.keys(this.accumulatedMeanGrads).forEach(function(t){return e.accumulatedMeanGrads[t].dispose()}),null!=this.accumulatedMoments&&Object.keys(this.accumulatedMoments).forEach(function(t){return e.accumulatedMoments[t].dispose()});},t.prototype.getConfig=function(){return {learningRate:this.learningRate,decay:this.decay,momentum:this.momentum,epsilon:this.epsilon,centered:this.centered}},t.fromConfig=function(e,t){return new e(t.learningRate,t.decay,t.momentum,t.epsilon,t.centered)},t.className="RMSPropOptimizer",t}(Optimizer);SerializationMap.register(RMSPropOptimizer);var OptimizerConstructors=function(){function e(){}return e.sgd=function(e){return new SGDOptimizer(e)},e.momentum=function(e,t,n){return void 0===n&&(n=!1),new MomentumOptimizer(e,t,n)},e.rmsprop=function(e,t,n,r,a){return void 0===t&&(t=.9),void 0===n&&(n=0),void 0===r&&(r=null),void 0===a&&(a=!1),new RMSPropOptimizer(e,t,n,r,a)},e.adam=function(e,t,n,r){return void 0===e&&(e=.001),void 0===t&&(t=.9),void 0===n&&(n=.999),void 0===r&&(r=null),new AdamOptimizer(e,t,n,r)},e.adadelta=function(e,t,n){return void 0===e&&(e=.001),void 0===t&&(t=.95),void 0===n&&(n=null),new AdadeltaOptimizer(e,t,n)},e.adamax=function(e,t,n,r,a){return void 0===e&&(e=.002),void 0===t&&(t=.9),void 0===n&&(n=.999),void 0===r&&(r=null),void 0===a&&(a=0),new AdamaxOptimizer(e,t,n,r,a)},e.adagrad=function(e,t){return void 0===t&&(t=.1),new AdagradOptimizer(e,t)},__decorate([doc({heading:"Training",subheading:"Optimizers",namespace:"train"})],e,"sgd",null),__decorate([doc({heading:"Training",subheading:"Optimizers",namespace:"train"})],e,"momentum",null),__decorate([doc({heading:"Training",subheading:"Optimizers",namespace:"train"})],e,"rmsprop",null),__decorate([doc({heading:"Training",subheading:"Optimizers",namespace:"train"})],e,"adam",null),__decorate([doc({heading:"Training",subheading:"Optimizers",namespace:"train"})],e,"adadelta",null),__decorate([doc({heading:"Training",subheading:"Optimizers",namespace:"train"})],e,"adamax",null),__decorate([doc({heading:"Training",subheading:"Optimizers",namespace:"train"})],e,"adagrad",null),e}(),train={sgd:OptimizerConstructors.sgd,momentum:OptimizerConstructors.momentum,adadelta:OptimizerConstructors.adadelta,adagrad:OptimizerConstructors.adagrad,rmsprop:OptimizerConstructors.rmsprop,adamax:OptimizerConstructors.adamax,adam:OptimizerConstructors.adam},setBackend=Environment.setBackend,getBackend=Environment.getBackend,disposeVariables=Environment.disposeVariables,memory=Environment.memory,nextFrame=BrowserUtil.nextFrame;
+
+ var tfCore_esm = /*#__PURE__*/Object.freeze({
+ setBackend: setBackend,
+ getBackend: getBackend,
+ disposeVariables: disposeVariables,
+ memory: memory,
+ version_core: version,
+ nextFrame: nextFrame,
+ environment: environment,
+ io: io,
+ serialization: serialization,
+ test_util: test_util,
+ util: util,
+ webgl: webgl,
+ AdadeltaOptimizer: AdadeltaOptimizer,
+ AdagradOptimizer: AdagradOptimizer,
+ AdamOptimizer: AdamOptimizer,
+ AdamaxOptimizer: AdamaxOptimizer,
+ MomentumOptimizer: MomentumOptimizer,
+ Optimizer: Optimizer,
+ RMSPropOptimizer: RMSPropOptimizer,
+ SGDOptimizer: SGDOptimizer,
+ Tensor: Tensor,
+ TensorBuffer: TensorBuffer,
+ variable: variable,
+ Variable: Variable,
+ get Rank () { return Rank; },
+ get Reduction () { return Reduction; },
+ ENV: ENV,
+ Environment: Environment,
+ doc: doc,
+ batchNormalization: batchNormalization,
+ batchNormalization2d: batchNormalization2d,
+ batchNormalization3d: batchNormalization3d,
+ batchNormalization4d: batchNormalization4d,
+ concat: concat,
+ concat1d: concat1d,
+ concat2d: concat2d,
+ concat3d: concat3d,
+ concat4d: concat4d,
+ conv1d: conv1d,
+ conv2d: conv2d,
+ conv2dTranspose: conv2dTranspose,
+ depthwiseConv2d: depthwiseConv2d,
+ separableConv2d: separableConv2d,
+ matMul: matMul,
+ matrixTimesVector: matrixTimesVector,
+ outerProduct: outerProduct,
+ vectorTimesMatrix: vectorTimesMatrix,
+ dot: dot,
+ avgPool: avgPool,
+ maxPool: maxPool,
+ transpose: transpose,
+ reverse: reverse,
+ reverse1d: reverse1d,
+ reverse2d: reverse2d,
+ reverse3d: reverse3d,
+ reverse4d: reverse4d,
+ slice: slice,
+ slice1d: slice1d,
+ slice2d: slice2d,
+ slice3d: slice3d,
+ slice4d: slice4d,
+ stridedSlice: stridedSlice,
+ argMax: argMax,
+ argMin: argMin,
+ logSumExp: logSumExp,
+ max: max,
+ mean: mean,
+ min: min,
+ all: all,
+ moments: moments,
+ sum: sum,
+ unsortedSegmentSum: unsortedSegmentSum,
+ equal: equal,
+ equalStrict: equalStrict,
+ greater: greater,
+ greaterStrict: greaterStrict,
+ greaterEqual: greaterEqual,
+ greaterEqualStrict: greaterEqualStrict,
+ less: less,
+ lessStrict: lessStrict,
+ lessEqual: lessEqual,
+ lessEqualStrict: lessEqualStrict,
+ notEqual: notEqual,
+ notEqualStrict: notEqualStrict,
+ logicalNot: logicalNot,
+ logicalAnd: logicalAnd,
+ logicalOr: logicalOr,
+ logicalXor: logicalXor,
+ where: where,
+ abs: abs,
+ acos: acos,
+ acosh: acosh,
+ asin: asin,
+ asinh: asinh,
+ atan: atan,
+ atanh: atanh,
+ ceil: ceil,
+ clipByValue: clipByValue,
+ cos: cos,
+ cosh: cosh,
+ elu: elu,
+ exp: exp,
+ expm1: expm1,
+ floor: floor,
+ sign: sign,
+ leakyRelu: leakyRelu,
+ log: log,
+ log1p: log1p,
+ logSigmoid: logSigmoid,
+ neg: neg,
+ prelu: prelu,
+ relu: relu,
+ reciprocal: reciprocal,
+ round: round,
+ selu: selu,
+ sigmoid: sigmoid,
+ sin: sin,
+ sinh: sinh,
+ softplus: softplus,
+ sqrt: sqrt,
+ rsqrt: rsqrt,
+ square: square,
+ step: step,
+ tan: tan,
+ tanh: tanh$1,
+ erf: erf,
+ add: add,
+ addStrict: addStrict,
+ atan2: atan2,
+ div: div,
+ floorDiv: floorDiv,
+ divStrict: divStrict,
+ maximum: maximum,
+ maximumStrict: maximumStrict,
+ minimum: minimum,
+ minimumStrict: minimumStrict,
+ mod: mod,
+ modStrict: modStrict,
+ mul: mul,
+ mulStrict: mulStrict,
+ pow: pow,
+ powStrict: powStrict,
+ sub: sub,
+ subStrict: subStrict,
+ squaredDifference: squaredDifference,
+ squaredDifferenceStrict: squaredDifferenceStrict,
+ norm: norm,
+ cast: cast,
+ clone: clone,
+ fromPixels: fromPixels,
+ toPixels: toPixels,
+ ones: ones,
+ onesLike: onesLike,
+ zeros: zeros,
+ zerosLike: zerosLike,
+ eye: eye,
+ rand: rand,
+ randomNormal: randomNormal,
+ truncatedNormal: truncatedNormal,
+ randomUniform: randomUniform,
+ multinomial: multinomial,
+ reshape: reshape,
+ squeeze: squeeze,
+ tile: tile,
+ gather: gather,
+ oneHot: oneHot,
+ linspace: linspace,
+ range: range,
+ buffer: buffer,
+ fill: fill,
+ tensor: tensor,
+ scalar: scalar,
+ tensor1d: tensor1d,
+ tensor2d: tensor2d,
+ tensor3d: tensor3d,
+ tensor4d: tensor4d,
+ tensor5d: tensor5d,
+ tensor6d: tensor6d,
+ print: print,
+ expandDims: expandDims,
+ stack: stack,
+ unstack: unstack,
+ split: split,
+ cumsum: cumsum,
+ pad: pad,
+ pad1d: pad1d,
+ pad2d: pad2d,
+ pad3d: pad3d,
+ pad4d: pad4d,
+ movingAverage: movingAverage,
+ basicLSTMCell: basicLSTMCell,
+ multiRNNCell: multiRNNCell,
+ softmax: softmax,
+ sigmoidCrossEntropyWithLogits: sigmoidCrossEntropyWithLogits,
+ localResponseNormalization: localResponseNormalization,
+ linalg: linalg,
+ losses: losses,
+ image: image,
+ operation: operation,
+ train: train,
+ tidy: tidy,
+ keep: keep,
+ dispose: dispose,
+ time: time,
+ grad: grad,
+ valueAndGrad: valueAndGrad,
+ grads: grads,
+ valueAndGrads: valueAndGrads,
+ variableGrads: variableGrads,
+ customGrad: customGrad
+ });
+
+ function euclideanDistance(arr1, arr2) {
+ if (arr1.length !== arr2.length)
+ throw new Error('euclideanDistance: arr1.length !== arr2.length');
+ var desc1 = Array.from(arr1);
+ var desc2 = Array.from(arr2);
+ return Math.sqrt(desc1
+ .map(function (val, i) { return val - desc2[i]; })
+ .reduce(function (res, diff) { return res + Math.pow(diff, 2); }, 0));
+ }
+
+ /*! *****************************************************************************
+ Copyright (c) Microsoft Corporation. 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
+
+ THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
+ WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
+ MERCHANTABLITY OR NON-INFRINGEMENT.
+
+ See the Apache Version 2.0 License for specific language governing permissions
+ and limitations under the License.
+ ***************************************************************************** */
+
+ function __awaiter$1(thisArg, _arguments, P, generator) {
+ return new (P || (P = Promise))(function (resolve, reject) {
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
+ function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
+ });
+ }
+
+ function __generator$1(thisArg, body) {
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
+ function verb(n) { return function (v) { return step([n, v]); }; }
+ function step(op) {
+ if (f) throw new TypeError("Generator is already executing.");
+ while (_) try {
+ if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
+ if (y = 0, t) op = [0, t.value];
+ switch (op[0]) {
+ case 0: case 1: t = op; break;
+ case 4: _.label++; return { value: op[1], done: false };
+ case 5: _.label++; y = op[1]; op = [0]; continue;
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
+ default:
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
+ if (t[2]) _.ops.pop();
+ _.trys.pop(); continue;
+ }
+ op = body.call(thisArg, _);
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
+ }
+ }
+
+ function isFloat(num) {
+ return num % 1 !== 0;
+ }
+ function isEven(num) {
+ return num % 2 === 0;
+ }
+ function round$1(num) {
+ return Math.floor(num * 100) / 100;
+ }
+ function getElement(arg) {
+ if (typeof arg === 'string') {
+ return document.getElementById(arg);
+ }
+ return arg;
+ }
+ function getContext2dOrThrow(canvas) {
+ var ctx = canvas.getContext('2d');
+ if (!ctx) {
+ throw new Error('canvas 2d context is null');
+ }
+ return ctx;
+ }
+ function createCanvas(_a) {
+ var width = _a.width, height = _a.height;
+ var canvas = document.createElement('canvas');
+ canvas.width = width;
+ canvas.height = height;
+ return canvas;
+ }
+ function createCanvasFromMedia(media, dims) {
+ var _a = dims || getMediaDimensions(media), width = _a.width, height = _a.height;
+ var canvas = createCanvas({ width: width, height: height });
+ getContext2dOrThrow(canvas).drawImage(media, 0, 0, width, height);
+ return canvas;
+ }
+ function getMediaDimensions(media) {
+ if (media instanceof HTMLImageElement) {
+ return { width: media.naturalWidth, height: media.naturalHeight };
+ }
+ if (media instanceof HTMLVideoElement) {
+ return { width: media.videoWidth, height: media.videoHeight };
+ }
+ return media;
+ }
+ function bufferToImage(buf) {
+ return new Promise(function (resolve, reject) {
+ if (!(buf instanceof Blob)) {
+ return reject('bufferToImage - expected buf to be of type: Blob');
+ }
+ var reader = new FileReader();
+ reader.onload = function () {
+ var img = new Image();
+ img.onload = function () { return resolve(img); };
+ img.onerror = reject;
+ img.src = reader.result;
+ };
+ reader.onerror = reject;
+ reader.readAsDataURL(buf);
+ });
+ }
+ function imageTensorToCanvas(imgTensor, canvas) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var targetCanvas, _a, _, height, width, numChannels;
+ return __generator$1(this, function (_b) {
+ switch (_b.label) {
+ case 0:
+ targetCanvas = canvas || document.createElement('canvas');
+ _a = imgTensor.shape, _ = _a[0], height = _a[1], width = _a[2], numChannels = _a[3];
+ return [4 /*yield*/, toPixels(imgTensor.as3D(height, width, numChannels).toInt(), targetCanvas)];
+ case 1:
+ _b.sent();
+ return [2 /*return*/, targetCanvas];
+ }
+ });
+ });
+ }
+ function getDefaultDrawOptions() {
+ return {
+ color: 'blue',
+ lineWidth: 2,
+ fontSize: 20,
+ fontStyle: 'Georgia'
};
-
- // Apply the seeding algorithm from Baagoe.
- me.c = 1;
- me.s0 = mash(' ');
- me.s1 = mash(' ');
- me.s2 = mash(' ');
- me.s0 -= mash(seed);
- if (me.s0 < 0) { me.s0 += 1; }
- me.s1 -= mash(seed);
- if (me.s1 < 0) { me.s1 += 1; }
- me.s2 -= mash(seed);
- if (me.s2 < 0) { me.s2 += 1; }
- mash = null;
- }
-
- function copy(f, t) {
- t.c = f.c;
- t.s0 = f.s0;
- t.s1 = f.s1;
- t.s2 = f.s2;
- return t;
- }
-
- function impl(seed, opts) {
- var xg = new Alea(seed),
- state = opts && opts.state,
- prng = xg.next;
- prng.int32 = function() { return (xg.next() * 0x100000000) | 0; };
- prng.double = function() {
- return prng() + (prng() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
+ }
+ function drawBox(ctx, x, y, w, h, options) {
+ var drawOptions = Object.assign(getDefaultDrawOptions(), (options || {}));
+ ctx.strokeStyle = drawOptions.color;
+ ctx.lineWidth = drawOptions.lineWidth;
+ ctx.strokeRect(x, y, w, h);
+ }
+ function drawText(ctx, x, y, text, options) {
+ var drawOptions = Object.assign(getDefaultDrawOptions(), (options || {}));
+ var padText = 2 + drawOptions.lineWidth;
+ ctx.fillStyle = drawOptions.color;
+ ctx.font = drawOptions.fontSize + "px " + drawOptions.fontStyle;
+ ctx.fillText(text, x + padText, y + padText + (drawOptions.fontSize * 0.6));
+ }
+ function drawDetection(canvasArg, detection, options) {
+ var canvas = getElement(canvasArg);
+ if (!(canvas instanceof HTMLCanvasElement)) {
+ throw new Error('drawBox - expected canvas to be of type: HTMLCanvasElement');
+ }
+ var detectionArray = Array.isArray(detection)
+ ? detection
+ : [detection];
+ detectionArray.forEach(function (det) {
+ var _a = det.getBox(), x = _a.x, y = _a.y, width = _a.width, height = _a.height;
+ var drawOptions = Object.assign(getDefaultDrawOptions(), (options || {}));
+ var withScore = Object.assign({ withScore: true }, (options || {})).withScore;
+ var ctx = getContext2dOrThrow(canvas);
+ drawBox(ctx, x, y, width, height, drawOptions);
+ if (withScore) {
+ drawText(ctx, x, y, "" + round$1(det.getScore()), drawOptions);
+ }
+ });
+ }
+ function drawContour(ctx, points, isClosed) {
+ if (isClosed === void 0) { isClosed = false; }
+ ctx.beginPath();
+ points.slice(1).forEach(function (_a, prevIdx) {
+ var x = _a.x, y = _a.y;
+ var from = points[prevIdx];
+ ctx.moveTo(from.x, from.y);
+ ctx.lineTo(x, y);
+ });
+ if (isClosed) {
+ var from = points[points.length - 1];
+ var to = points[0];
+ if (!from || !to) {
+ return;
+ }
+ ctx.moveTo(from.x, from.y);
+ ctx.lineTo(to.x, to.y);
+ }
+ ctx.stroke();
+ }
+ function drawLandmarks(canvasArg, faceLandmarks, options) {
+ var canvas = getElement(canvasArg);
+ if (!(canvas instanceof HTMLCanvasElement)) {
+ throw new Error('drawLandmarks - expected canvas to be of type: HTMLCanvasElement');
+ }
+ var drawOptions = Object.assign(getDefaultDrawOptions(), (options || {}));
+ var drawLines = Object.assign({ drawLines: false }, (options || {})).drawLines;
+ var ctx = getContext2dOrThrow(canvas);
+ var lineWidth = drawOptions.lineWidth, color = drawOptions.color;
+ if (drawLines) {
+ ctx.strokeStyle = color;
+ ctx.lineWidth = lineWidth;
+ drawContour(ctx, faceLandmarks.getJawOutline());
+ drawContour(ctx, faceLandmarks.getLeftEyeBrow());
+ drawContour(ctx, faceLandmarks.getRightEyeBrow());
+ drawContour(ctx, faceLandmarks.getNose());
+ drawContour(ctx, faceLandmarks.getLeftEye(), true);
+ drawContour(ctx, faceLandmarks.getRightEye(), true);
+ drawContour(ctx, faceLandmarks.getMouth(), true);
+ return;
+ }
+ // else draw points
+ var ptOffset = lineWidth / 2;
+ ctx.fillStyle = color;
+ faceLandmarks.getPositions().forEach(function (pt) { return ctx.fillRect(pt.x - ptOffset, pt.y - ptOffset, lineWidth, lineWidth); });
+ }
+
+ var NetInput = /** @class */ (function () {
+ function NetInput(mediaArg, dims) {
+ var _this = this;
+ var mediaArgArray = Array.isArray(mediaArg)
+ ? mediaArg
+ : [mediaArg];
+ if (!mediaArgArray.length) {
+ throw new Error('NetInput - empty array passed as input');
+ }
+ var medias = mediaArgArray.map(getElement);
+ medias.forEach(function (media, i) {
+ if (!(media instanceof HTMLImageElement || media instanceof HTMLVideoElement || media instanceof HTMLCanvasElement)) {
+ var idxHint = Array.isArray(mediaArg) ? " at input index " + i + ":" : '';
+ if (typeof mediaArgArray[i] === 'string') {
+ throw new Error("NetInput -" + idxHint + " string passed, but could not resolve HTMLElement for element id");
+ }
+ throw new Error("NetInput -" + idxHint + " expected media to be of type HTMLImageElement | HTMLVideoElement | HTMLCanvasElement, or to be an element id");
+ }
+ });
+ this._canvases = [];
+ medias.forEach(function (m) { return _this.initCanvas(m, dims); });
+ }
+ NetInput.prototype.initCanvas = function (media, dims) {
+ if (media instanceof HTMLCanvasElement) {
+ this._canvases.push(media);
+ return;
+ }
+ // if input is batch type, make sure every canvas has the same dimensions
+ var canvasDims = this.dims || dims;
+ this._canvases.push(createCanvasFromMedia(media, canvasDims));
};
- prng.quick = prng;
- if (state) {
- if (typeof(state) == 'object') copy(state, xg);
- prng.state = function() { return copy(xg, {}); };
+ Object.defineProperty(NetInput.prototype, "canvases", {
+ get: function () {
+ return this._canvases;
+ },
+ enumerable: true,
+ configurable: true
+ });
+ Object.defineProperty(NetInput.prototype, "width", {
+ get: function () {
+ return (this._canvases[0] || {}).width;
+ },
+ enumerable: true,
+ configurable: true
+ });
+ Object.defineProperty(NetInput.prototype, "height", {
+ get: function () {
+ return (this._canvases[0] || {}).height;
+ },
+ enumerable: true,
+ configurable: true
+ });
+ Object.defineProperty(NetInput.prototype, "dims", {
+ get: function () {
+ var _a = this, width = _a.width, height = _a.height;
+ return (width > 0 && height > 0) ? { width: width, height: height } : null;
+ },
+ enumerable: true,
+ configurable: true
+ });
+ return NetInput;
+ }());
+
+ /**
+ * Pads the smaller dimension of an image tensor with zeros, such that width === height.
+ *
+ * @param imgTensor The image tensor.
+ * @param isCenterImage (optional, default: false) If true, add padding on both sides of the image, such that the image.
+ * @returns The padded tensor with width === height.
+ */
+ function padToSquare(imgTensor, isCenterImage) {
+ if (isCenterImage === void 0) { isCenterImage = false; }
+ return tidy(function () {
+ var _a = imgTensor.shape.slice(1), height = _a[0], width = _a[1];
+ if (height === width) {
+ return imgTensor;
+ }
+ var dimDiff = Math.abs(height - width);
+ var paddingAmount = Math.floor(dimDiff * (isCenterImage ? 0.5 : 1));
+ var paddingAxis = height > width ? 2 : 1;
+ var getPaddingTensorShape = function (isRoundUp) {
+ if (isRoundUp === void 0) { isRoundUp = false; }
+ var paddingTensorShape = imgTensor.shape.slice();
+ paddingTensorShape[paddingAxis] = paddingAmount + (isRoundUp ? 1 : 0);
+ return paddingTensorShape;
+ };
+ var tensorsToStack = (isCenterImage ? [fill(getPaddingTensorShape(!isEven(dimDiff)), 0)] : [])
+ .concat([imgTensor, fill(getPaddingTensorShape(), 0)]);
+ return concat(tensorsToStack, paddingAxis);
+ });
+ }
+
+ var Rect = /** @class */ (function () {
+ function Rect(x, y, width, height) {
+ this.x = x;
+ this.y = y;
+ this.width = width;
+ this.height = height;
}
- return prng;
- }
-
- function Mash() {
- var n = 0xefc8249d;
-
- var mash = function(data) {
- data = data.toString();
- for (var i = 0; i < data.length; i++) {
- n += data.charCodeAt(i);
- var h = 0.02519603282416938 * n;
- n = h >>> 0;
- h -= n;
- h *= n;
- n = h >>> 0;
- h -= n;
- n += h * 0x100000000; // 2^32
- }
- return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
+ Rect.prototype.floor = function () {
+ return new Rect(Math.floor(this.x), Math.floor(this.y), Math.floor(this.width), Math.floor(this.height));
};
-
- return mash;
- }
-
-
- if (module && module.exports) {
- module.exports = impl;
- } else if (define && define.amd) {
- define(function() { return impl; });
- } else {
- this.alea = impl;
- }
-
- })(
- commonjsGlobal,
- module, // present in node.js
- (typeof undefined) == 'function' && undefined // present with an AMD loader
- );
- });
-
- var xor128 = createCommonjsModule(function (module) {
- // A Javascript implementaion of the "xor128" prng algorithm by
- // George Marsaglia. See http://www.jstatsoft.org/v08/i14/paper
-
- (function(global, module, define) {
-
- function XorGen(seed) {
- var me = this, strseed = '';
-
- me.x = 0;
- me.y = 0;
- me.z = 0;
- me.w = 0;
-
- // Set up generator function.
- me.next = function() {
- var t = me.x ^ (me.x << 11);
- me.x = me.y;
- me.y = me.z;
- me.z = me.w;
- return me.w ^= (me.w >>> 19) ^ t ^ (t >>> 8);
+ return Rect;
+ }());
+
+ var FaceDetection = /** @class */ (function () {
+ function FaceDetection(score, relativeBox, imageDims) {
+ var width = imageDims.width, height = imageDims.height;
+ this._imageWidth = width;
+ this._imageHeight = height;
+ this._score = score;
+ this._box = new Rect(relativeBox.x * width, relativeBox.y * height, relativeBox.width * width, relativeBox.height * height);
+ }
+ FaceDetection.prototype.getScore = function () {
+ return this._score;
};
-
- if (seed === (seed | 0)) {
- // Integer seed.
- me.x = seed;
- } else {
- // String seed.
- strseed += seed;
+ FaceDetection.prototype.getBox = function () {
+ return this._box;
+ };
+ FaceDetection.prototype.getImageWidth = function () {
+ return this._imageWidth;
+ };
+ FaceDetection.prototype.getImageHeight = function () {
+ return this._imageHeight;
+ };
+ FaceDetection.prototype.getRelativeBox = function () {
+ return new Rect(this._box.x / this._imageWidth, this._box.y / this._imageHeight, this._box.width / this._imageWidth, this._box.height / this._imageHeight);
+ };
+ FaceDetection.prototype.forSize = function (width, height) {
+ return new FaceDetection(this._score, this.getRelativeBox(), { width: width, height: height });
+ };
+ return FaceDetection;
+ }());
+
+ /**
+ * Extracts the image regions containing the detected faces.
+ *
+ * @param input The image that face detection has been performed on.
+ * @param detections The face detection results or face bounding boxes for that image.
+ * @returns The Canvases of the corresponding image region for each detected face.
+ */
+ function extractFaces(image, detections) {
+ var ctx = getContext2dOrThrow(image);
+ var boxes = detections.map(function (det) { return det instanceof FaceDetection
+ ? det.forSize(image.width, image.height).getBox().floor()
+ : det; });
+ return boxes.map(function (_a) {
+ var x = _a.x, y = _a.y, width = _a.width, height = _a.height;
+ var faceImg = createCanvas({ width: width, height: height });
+ getContext2dOrThrow(faceImg)
+ .putImageData(ctx.getImageData(x, y, width, height), 0, 0);
+ return faceImg;
+ });
+ }
+
+ function getImageTensor(input) {
+ return tidy(function () {
+ if (input instanceof Tensor) {
+ var rank = input.shape.length;
+ if (rank !== 3 && rank !== 4) {
+ throw new Error('input tensor must be of rank 3 or 4');
+ }
+ return (rank === 3 ? input.expandDims(0) : input).toFloat();
+ }
+ var netInput = input instanceof NetInput ? input : new NetInput(input);
+ return concat(netInput.canvases.map(function (canvas) {
+ return fromPixels(canvas).expandDims(0).toFloat();
+ }));
+ });
+ }
+
+ /**
+ * Extracts the tensors of the image regions containing the detected faces.
+ * Useful if you want to compute the face descriptors for the face images.
+ * Using this method is faster then extracting a canvas for each face and
+ * converting them to tensors individually.
+ *
+ * @param input The image that face detection has been performed on.
+ * @param detections The face detection results or face bounding boxes for that image.
+ * @returns Tensors of the corresponding image region for each detected face.
+ */
+ function extractFaceTensors(image$$1, detections) {
+ return tidy(function () {
+ var imgTensor = getImageTensor(image$$1);
+ // TODO handle batches
+ var _a = imgTensor.shape, batchSize = _a[0], imgHeight = _a[1], imgWidth = _a[2], numChannels = _a[3];
+ var boxes = detections.map(function (det) { return det instanceof FaceDetection
+ ? det.forSize(imgWidth, imgHeight).getBox().floor()
+ : det; });
+ var faceTensors = boxes.map(function (_a) {
+ var x = _a.x, y = _a.y, width = _a.width, height = _a.height;
+ return slice(imgTensor, [0, y, x, 0], [1, height, width, numChannels]);
+ });
+ return faceTensors;
+ });
+ }
+
+ function extractWeightsFactory(weights) {
+ var remainingWeights = weights;
+ function extractWeights(numWeights) {
+ var ret = remainingWeights.slice(0, numWeights);
+ remainingWeights = remainingWeights.slice(numWeights);
+ return ret;
}
-
- // Mix in string seed, then discard an initial batch of 64 values.
- for (var k = 0; k < strseed.length + 64; k++) {
- me.x ^= strseed.charCodeAt(k) | 0;
- me.next();
+ function getRemainingWeights() {
+ return remainingWeights;
}
- }
-
- function copy(f, t) {
- t.x = f.x;
- t.y = f.y;
- t.z = f.z;
- t.w = f.w;
- return t;
- }
-
- function impl(seed, opts) {
- var xg = new XorGen(seed),
- state = opts && opts.state,
- prng = function() { return (xg.next() >>> 0) / 0x100000000; };
- prng.double = function() {
- do {
- var top = xg.next() >>> 11,
- bot = (xg.next() >>> 0) / 0x100000000,
- result = (top + bot) / (1 << 21);
- } while (result === 0);
- return result;
+ return {
+ extractWeights: extractWeights,
+ getRemainingWeights: getRemainingWeights
};
- prng.int32 = xg.next;
- prng.quick = prng;
- if (state) {
- if (typeof(state) == 'object') copy(state, xg);
- prng.state = function() { return copy(xg, {}); };
+ }
+
+ function extractorsFactory(extractWeights) {
+ function extractDepthwiseConvParams(numChannels) {
+ var filters = tensor4d(extractWeights(3 * 3 * numChannels), [3, 3, numChannels, 1]);
+ var batch_norm_scale = tensor1d(extractWeights(numChannels));
+ var batch_norm_offset = tensor1d(extractWeights(numChannels));
+ var batch_norm_mean = tensor1d(extractWeights(numChannels));
+ var batch_norm_variance = tensor1d(extractWeights(numChannels));
+ return {
+ filters: filters,
+ batch_norm_scale: batch_norm_scale,
+ batch_norm_offset: batch_norm_offset,
+ batch_norm_mean: batch_norm_mean,
+ batch_norm_variance: batch_norm_variance
+ };
}
- return prng;
- }
-
- if (module && module.exports) {
- module.exports = impl;
- } else if (define && define.amd) {
- define(function() { return impl; });
- } else {
- this.xor128 = impl;
- }
-
- })(
- commonjsGlobal,
- module, // present in node.js
- (typeof undefined) == 'function' && undefined // present with an AMD loader
- );
- });
-
- var xorwow = createCommonjsModule(function (module) {
- // A Javascript implementaion of the "xorwow" prng algorithm by
- // George Marsaglia. See http://www.jstatsoft.org/v08/i14/paper
-
- (function(global, module, define) {
-
- function XorGen(seed) {
- var me = this, strseed = '';
-
- // Set up generator function.
- me.next = function() {
- var t = (me.x ^ (me.x >>> 2));
- me.x = me.y; me.y = me.z; me.z = me.w; me.w = me.v;
- return (me.d = (me.d + 362437 | 0)) +
- (me.v = (me.v ^ (me.v << 4)) ^ (t ^ (t << 1))) | 0;
+ function extractConvParams(channelsIn, channelsOut, filterSize) {
+ var filters = tensor4d(extractWeights(channelsIn * channelsOut * filterSize * filterSize), [filterSize, filterSize, channelsIn, channelsOut]);
+ var bias = tensor1d(extractWeights(channelsOut));
+ return {
+ filters: filters,
+ bias: bias
+ };
+ }
+ function extractPointwiseConvParams(channelsIn, channelsOut, filterSize) {
+ var _a = extractConvParams(channelsIn, channelsOut, filterSize), filters = _a.filters, bias = _a.bias;
+ return {
+ filters: filters,
+ batch_norm_offset: bias
+ };
+ }
+ function extractConvPairParams(channelsIn, channelsOut) {
+ var depthwise_conv_params = extractDepthwiseConvParams(channelsIn);
+ var pointwise_conv_params = extractPointwiseConvParams(channelsIn, channelsOut, 1);
+ return {
+ depthwise_conv_params: depthwise_conv_params,
+ pointwise_conv_params: pointwise_conv_params
+ };
+ }
+ function extractMobilenetV1Params() {
+ var conv_0_params = extractPointwiseConvParams(3, 32, 3);
+ var channelNumPairs = [
+ [32, 64],
+ [64, 128],
+ [128, 128],
+ [128, 256],
+ [256, 256],
+ [256, 512],
+ [512, 512],
+ [512, 512],
+ [512, 512],
+ [512, 512],
+ [512, 512],
+ [512, 1024],
+ [1024, 1024]
+ ];
+ var conv_pair_params = channelNumPairs.map(function (_a) {
+ var channelsIn = _a[0], channelsOut = _a[1];
+ return extractConvPairParams(channelsIn, channelsOut);
+ });
+ return {
+ conv_0_params: conv_0_params,
+ conv_pair_params: conv_pair_params
+ };
+ }
+ function extractPredictionLayerParams() {
+ var conv_0_params = extractPointwiseConvParams(1024, 256, 1);
+ var conv_1_params = extractPointwiseConvParams(256, 512, 3);
+ var conv_2_params = extractPointwiseConvParams(512, 128, 1);
+ var conv_3_params = extractPointwiseConvParams(128, 256, 3);
+ var conv_4_params = extractPointwiseConvParams(256, 128, 1);
+ var conv_5_params = extractPointwiseConvParams(128, 256, 3);
+ var conv_6_params = extractPointwiseConvParams(256, 64, 1);
+ var conv_7_params = extractPointwiseConvParams(64, 128, 3);
+ var box_encoding_0_predictor_params = extractConvParams(512, 12, 1);
+ var class_predictor_0_params = extractConvParams(512, 9, 1);
+ var box_encoding_1_predictor_params = extractConvParams(1024, 24, 1);
+ var class_predictor_1_params = extractConvParams(1024, 18, 1);
+ var box_encoding_2_predictor_params = extractConvParams(512, 24, 1);
+ var class_predictor_2_params = extractConvParams(512, 18, 1);
+ var box_encoding_3_predictor_params = extractConvParams(256, 24, 1);
+ var class_predictor_3_params = extractConvParams(256, 18, 1);
+ var box_encoding_4_predictor_params = extractConvParams(256, 24, 1);
+ var class_predictor_4_params = extractConvParams(256, 18, 1);
+ var box_encoding_5_predictor_params = extractConvParams(128, 24, 1);
+ var class_predictor_5_params = extractConvParams(128, 18, 1);
+ var box_predictor_0_params = {
+ box_encoding_predictor_params: box_encoding_0_predictor_params,
+ class_predictor_params: class_predictor_0_params
+ };
+ var box_predictor_1_params = {
+ box_encoding_predictor_params: box_encoding_1_predictor_params,
+ class_predictor_params: class_predictor_1_params
+ };
+ var box_predictor_2_params = {
+ box_encoding_predictor_params: box_encoding_2_predictor_params,
+ class_predictor_params: class_predictor_2_params
+ };
+ var box_predictor_3_params = {
+ box_encoding_predictor_params: box_encoding_3_predictor_params,
+ class_predictor_params: class_predictor_3_params
+ };
+ var box_predictor_4_params = {
+ box_encoding_predictor_params: box_encoding_4_predictor_params,
+ class_predictor_params: class_predictor_4_params
+ };
+ var box_predictor_5_params = {
+ box_encoding_predictor_params: box_encoding_5_predictor_params,
+ class_predictor_params: class_predictor_5_params
+ };
+ return {
+ conv_0_params: conv_0_params,
+ conv_1_params: conv_1_params,
+ conv_2_params: conv_2_params,
+ conv_3_params: conv_3_params,
+ conv_4_params: conv_4_params,
+ conv_5_params: conv_5_params,
+ conv_6_params: conv_6_params,
+ conv_7_params: conv_7_params,
+ box_predictor_0_params: box_predictor_0_params,
+ box_predictor_1_params: box_predictor_1_params,
+ box_predictor_2_params: box_predictor_2_params,
+ box_predictor_3_params: box_predictor_3_params,
+ box_predictor_4_params: box_predictor_4_params,
+ box_predictor_5_params: box_predictor_5_params
+ };
+ }
+ return {
+ extractMobilenetV1Params: extractMobilenetV1Params,
+ extractPredictionLayerParams: extractPredictionLayerParams
};
-
- me.x = 0;
- me.y = 0;
- me.z = 0;
- me.w = 0;
- me.v = 0;
-
- if (seed === (seed | 0)) {
- // Integer seed.
- me.x = seed;
- } else {
- // String seed.
- strseed += seed;
+ }
+ function extractParams(weights) {
+ var _a = extractWeightsFactory(weights), extractWeights = _a.extractWeights, getRemainingWeights = _a.getRemainingWeights;
+ var _b = extractorsFactory(extractWeights), extractMobilenetV1Params = _b.extractMobilenetV1Params, extractPredictionLayerParams = _b.extractPredictionLayerParams;
+ var mobilenetv1_params = extractMobilenetV1Params();
+ var prediction_layer_params = extractPredictionLayerParams();
+ var extra_dim = tensor3d(extractWeights(5118 * 4), [1, 5118, 4]);
+ var output_layer_params = {
+ extra_dim: extra_dim
+ };
+ if (getRemainingWeights().length !== 0) {
+ throw new Error("weights remaing after extract: " + getRemainingWeights().length);
}
-
- // Mix in string seed, then discard an initial batch of 64 values.
- for (var k = 0; k < strseed.length + 64; k++) {
- me.x ^= strseed.charCodeAt(k) | 0;
- if (k == strseed.length) {
- me.d = me.x << 10 ^ me.x >>> 4;
- }
- me.next();
+ return {
+ mobilenetv1_params: mobilenetv1_params,
+ prediction_layer_params: prediction_layer_params,
+ output_layer_params: output_layer_params
+ };
+ }
+
+ function isTensor(tensor$$1, dim) {
+ return tensor$$1 instanceof Tensor && tensor$$1.shape.length === dim;
+ }
+ function isTensor1D(tensor$$1) {
+ return isTensor(tensor$$1, 1);
+ }
+ function isTensor2D(tensor$$1) {
+ return isTensor(tensor$$1, 2);
+ }
+ function isTensor3D(tensor$$1) {
+ return isTensor(tensor$$1, 3);
+ }
+ function isTensor4D(tensor$$1) {
+ return isTensor(tensor$$1, 4);
+ }
+
+ function getModelUris(uri, defaultModelName) {
+ var parts = (uri || '').split('/');
+ var modelBaseUri = ((uri || '').endsWith('.json')
+ ? parts.slice(0, parts.length - 1)
+ : parts).filter(function (s) { return s; }).join('/');
+ var defaultManifestFilename = defaultModelName + "-weights_manifest.json";
+ var manifestUri = !uri || !modelBaseUri
+ ? defaultManifestFilename
+ : (uri.endsWith('.json')
+ ? uri
+ : modelBaseUri + "/" + defaultManifestFilename);
+ return { manifestUri: manifestUri, modelBaseUri: modelBaseUri };
+ }
+ function loadWeightMap(uri, defaultModelName) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var _a, manifestUri, modelBaseUri, manifest;
+ return __generator$1(this, function (_b) {
+ switch (_b.label) {
+ case 0:
+ _a = getModelUris(uri, defaultModelName), manifestUri = _a.manifestUri, modelBaseUri = _a.modelBaseUri;
+ return [4 /*yield*/, fetch(manifestUri)];
+ case 1: return [4 /*yield*/, (_b.sent()).json()];
+ case 2:
+ manifest = _b.sent();
+ return [2 /*return*/, io.loadWeights(manifest, modelBaseUri)];
+ }
+ });
+ });
+ }
+
+ var DEFAULT_MODEL_NAME = 'face_detection_model';
+ function extractorsFactory$1(weightMap) {
+ function extractPointwiseConvParams(prefix, idx) {
+ var pointwise_conv_params = {
+ filters: weightMap[prefix + "/Conv2d_" + idx + "_pointwise/weights"],
+ batch_norm_offset: weightMap[prefix + "/Conv2d_" + idx + "_pointwise/convolution_bn_offset"]
+ };
+ if (!isTensor4D(pointwise_conv_params.filters)) {
+ throw new Error("expected weightMap[" + prefix + "/Conv2d_" + idx + "_pointwise/weights] to be a Tensor4D, instead have " + pointwise_conv_params.filters);
+ }
+ if (!isTensor1D(pointwise_conv_params.batch_norm_offset)) {
+ throw new Error("expected weightMap[" + prefix + "/Conv2d_" + idx + "_pointwise/convolution_bn_offset] to be a Tensor1D, instead have " + pointwise_conv_params.batch_norm_offset);
+ }
+ return pointwise_conv_params;
}
- }
-
- function copy(f, t) {
- t.x = f.x;
- t.y = f.y;
- t.z = f.z;
- t.w = f.w;
- t.v = f.v;
- t.d = f.d;
- return t;
- }
-
- function impl(seed, opts) {
- var xg = new XorGen(seed),
- state = opts && opts.state,
- prng = function() { return (xg.next() >>> 0) / 0x100000000; };
- prng.double = function() {
- do {
- var top = xg.next() >>> 11,
- bot = (xg.next() >>> 0) / 0x100000000,
- result = (top + bot) / (1 << 21);
- } while (result === 0);
- return result;
+ function extractConvPairParams(idx) {
+ var depthwise_conv_params = {
+ filters: weightMap["MobilenetV1/Conv2d_" + idx + "_depthwise/depthwise_weights"],
+ batch_norm_scale: weightMap["MobilenetV1/Conv2d_" + idx + "_depthwise/BatchNorm/gamma"],
+ batch_norm_offset: weightMap["MobilenetV1/Conv2d_" + idx + "_depthwise/BatchNorm/beta"],
+ batch_norm_mean: weightMap["MobilenetV1/Conv2d_" + idx + "_depthwise/BatchNorm/moving_mean"],
+ batch_norm_variance: weightMap["MobilenetV1/Conv2d_" + idx + "_depthwise/BatchNorm/moving_variance"],
+ };
+ if (!isTensor4D(depthwise_conv_params.filters)) {
+ throw new Error("expected weightMap[MobilenetV1/Conv2d_" + idx + "_depthwise/depthwise_weights] to be a Tensor4D, instead have " + depthwise_conv_params.filters);
+ }
+ if (!isTensor1D(depthwise_conv_params.batch_norm_scale)) {
+ throw new Error("expected weightMap[MobilenetV1/Conv2d_" + idx + "_depthwise/BatchNorm/gamma] to be a Tensor1D, instead have " + depthwise_conv_params.batch_norm_scale);
+ }
+ if (!isTensor1D(depthwise_conv_params.batch_norm_offset)) {
+ throw new Error("expected weightMap[MobilenetV1/Conv2d_" + idx + "_depthwise/BatchNorm/beta] to be a Tensor1D, instead have " + depthwise_conv_params.batch_norm_offset);
+ }
+ if (!isTensor1D(depthwise_conv_params.batch_norm_mean)) {
+ throw new Error("expected weightMap[MobilenetV1/Conv2d_" + idx + "_depthwise/BatchNorm/moving_mean] to be a Tensor1D, instead have " + depthwise_conv_params.batch_norm_mean);
+ }
+ if (!isTensor1D(depthwise_conv_params.batch_norm_variance)) {
+ throw new Error("expected weightMap[MobilenetV1/Conv2d_" + idx + "_depthwise/BatchNorm/moving_variance] to be a Tensor1D, instead have " + depthwise_conv_params.batch_norm_variance);
+ }
+ return {
+ depthwise_conv_params: depthwise_conv_params,
+ pointwise_conv_params: extractPointwiseConvParams('MobilenetV1', idx)
+ };
+ }
+ function extractMobilenetV1Params() {
+ return {
+ conv_0_params: extractPointwiseConvParams('MobilenetV1', 0),
+ conv_pair_params: Array(13).fill(0).map(function (_, i) { return extractConvPairParams(i + 1); })
+ };
+ }
+ function extractBoxPredictorParams(idx) {
+ var params = {
+ box_encoding_predictor_params: {
+ filters: weightMap["Prediction/BoxPredictor_" + idx + "/BoxEncodingPredictor/weights"],
+ bias: weightMap["Prediction/BoxPredictor_" + idx + "/BoxEncodingPredictor/biases"]
+ },
+ class_predictor_params: {
+ filters: weightMap["Prediction/BoxPredictor_" + idx + "/ClassPredictor/weights"],
+ bias: weightMap["Prediction/BoxPredictor_" + idx + "/ClassPredictor/biases"]
+ }
+ };
+ if (!isTensor4D(params.box_encoding_predictor_params.filters)) {
+ throw new Error("expected weightMap[Prediction/BoxPredictor_" + idx + "/BoxEncodingPredictor/weights] to be a Tensor4D, instead have " + params.box_encoding_predictor_params.filters);
+ }
+ if (!isTensor1D(params.box_encoding_predictor_params.bias)) {
+ throw new Error("expected weightMap[Prediction/BoxPredictor_" + idx + "/BoxEncodingPredictor/biases] to be a Tensor1D, instead have " + params.box_encoding_predictor_params.bias);
+ }
+ if (!isTensor4D(params.class_predictor_params.filters)) {
+ throw new Error("expected weightMap[Prediction/BoxPredictor_" + idx + "/ClassPredictor/weights] to be a Tensor4D, instead have " + params.class_predictor_params.filters);
+ }
+ if (!isTensor1D(params.class_predictor_params.bias)) {
+ throw new Error("expected weightMap[Prediction/BoxPredictor_" + idx + "/ClassPredictor/biases] to be a Tensor1D, instead have " + params.class_predictor_params.bias);
+ }
+ return params;
+ }
+ function extractPredictionLayerParams() {
+ return {
+ conv_0_params: extractPointwiseConvParams('Prediction', 0),
+ conv_1_params: extractPointwiseConvParams('Prediction', 1),
+ conv_2_params: extractPointwiseConvParams('Prediction', 2),
+ conv_3_params: extractPointwiseConvParams('Prediction', 3),
+ conv_4_params: extractPointwiseConvParams('Prediction', 4),
+ conv_5_params: extractPointwiseConvParams('Prediction', 5),
+ conv_6_params: extractPointwiseConvParams('Prediction', 6),
+ conv_7_params: extractPointwiseConvParams('Prediction', 7),
+ box_predictor_0_params: extractBoxPredictorParams(0),
+ box_predictor_1_params: extractBoxPredictorParams(1),
+ box_predictor_2_params: extractBoxPredictorParams(2),
+ box_predictor_3_params: extractBoxPredictorParams(3),
+ box_predictor_4_params: extractBoxPredictorParams(4),
+ box_predictor_5_params: extractBoxPredictorParams(5)
+ };
+ }
+ return {
+ extractMobilenetV1Params: extractMobilenetV1Params,
+ extractPredictionLayerParams: extractPredictionLayerParams
+ };
+ }
+ function loadQuantizedParams(uri) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var weightMap, _a, extractMobilenetV1Params, extractPredictionLayerParams, extra_dim;
+ return __generator$1(this, function (_b) {
+ switch (_b.label) {
+ case 0: return [4 /*yield*/, loadWeightMap(uri, DEFAULT_MODEL_NAME)];
+ case 1:
+ weightMap = _b.sent();
+ _a = extractorsFactory$1(weightMap), extractMobilenetV1Params = _a.extractMobilenetV1Params, extractPredictionLayerParams = _a.extractPredictionLayerParams;
+ extra_dim = weightMap['Output/extra_dim'];
+ if (!isTensor3D(extra_dim)) {
+ throw new Error("expected weightMap['Output/extra_dim'] to be a Tensor3D, instead have " + extra_dim);
+ }
+ return [2 /*return*/, {
+ mobilenetv1_params: extractMobilenetV1Params(),
+ prediction_layer_params: extractPredictionLayerParams(),
+ output_layer_params: {
+ extra_dim: extra_dim
+ }
+ }];
+ }
+ });
+ });
+ }
+
+ function pointwiseConvLayer(x, params, strides) {
+ return tidy(function () {
+ var out = conv2d(x, params.filters, strides, 'same');
+ out = add(out, params.batch_norm_offset);
+ return clipByValue(out, 0, 6);
+ });
+ }
+
+ var epsilon = 0.0010000000474974513;
+ function depthwiseConvLayer(x, params, strides) {
+ return tidy(function () {
+ var out = depthwiseConv2d(x, params.filters, strides, 'same');
+ out = batchNormalization(out, params.batch_norm_mean, params.batch_norm_variance, epsilon, params.batch_norm_scale, params.batch_norm_offset);
+ return clipByValue(out, 0, 6);
+ });
+ }
+ function getStridesForLayerIdx(layerIdx) {
+ return [2, 4, 6, 12].some(function (idx) { return idx === layerIdx; }) ? [2, 2] : [1, 1];
+ }
+ function mobileNetV1(x, params) {
+ return tidy(function () {
+ var conv11 = null;
+ var out = pointwiseConvLayer(x, params.conv_0_params, [2, 2]);
+ params.conv_pair_params.forEach(function (param, i) {
+ var layerIdx = i + 1;
+ var depthwiseConvStrides = getStridesForLayerIdx(layerIdx);
+ out = depthwiseConvLayer(out, param.depthwise_conv_params, depthwiseConvStrides);
+ out = pointwiseConvLayer(out, param.pointwise_conv_params, [1, 1]);
+ if (layerIdx === 11) {
+ conv11 = out;
+ }
+ });
+ if (conv11 === null) {
+ throw new Error('mobileNetV1 - output of conv layer 11 is null');
+ }
+ return {
+ out: out,
+ conv11: conv11
+ };
+ });
+ }
+
+ function nonMaxSuppression(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold) {
+ var numBoxes = boxes.shape[0];
+ var outputSize = Math.min(maxOutputSize, numBoxes);
+ var candidates = scores
+ .map(function (score, boxIndex) { return ({ score: score, boxIndex: boxIndex }); })
+ .filter(function (c) { return c.score > scoreThreshold; })
+ .sort(function (c1, c2) { return c2.score - c1.score; });
+ var suppressFunc = function (x) { return x <= iouThreshold ? 1 : 0; };
+ var selected = [];
+ candidates.forEach(function (c) {
+ if (selected.length >= outputSize) {
+ return;
+ }
+ var originalScore = c.score;
+ for (var j = selected.length - 1; j >= 0; --j) {
+ var iou = IOU(boxes, c.boxIndex, selected[j]);
+ if (iou === 0.0) {
+ continue;
+ }
+ c.score *= suppressFunc(iou);
+ if (c.score <= scoreThreshold) {
+ break;
+ }
+ }
+ if (originalScore === c.score) {
+ selected.push(c.boxIndex);
+ }
+ });
+ return selected;
+ }
+ function IOU(boxes, i, j) {
+ var yminI = Math.min(boxes.get(i, 0), boxes.get(i, 2));
+ var xminI = Math.min(boxes.get(i, 1), boxes.get(i, 3));
+ var ymaxI = Math.max(boxes.get(i, 0), boxes.get(i, 2));
+ var xmaxI = Math.max(boxes.get(i, 1), boxes.get(i, 3));
+ var yminJ = Math.min(boxes.get(j, 0), boxes.get(j, 2));
+ var xminJ = Math.min(boxes.get(j, 1), boxes.get(j, 3));
+ var ymaxJ = Math.max(boxes.get(j, 0), boxes.get(j, 2));
+ var xmaxJ = Math.max(boxes.get(j, 1), boxes.get(j, 3));
+ var areaI = (ymaxI - yminI) * (xmaxI - xminI);
+ var areaJ = (ymaxJ - yminJ) * (xmaxJ - xminJ);
+ if (areaI <= 0 || areaJ <= 0) {
+ return 0.0;
+ }
+ var intersectionYmin = Math.max(yminI, yminJ);
+ var intersectionXmin = Math.max(xminI, xminJ);
+ var intersectionYmax = Math.min(ymaxI, ymaxJ);
+ var intersectionXmax = Math.min(xmaxI, xmaxJ);
+ var intersectionArea = Math.max(intersectionYmax - intersectionYmin, 0.0) *
+ Math.max(intersectionXmax - intersectionXmin, 0.0);
+ return intersectionArea / (areaI + areaJ - intersectionArea);
+ }
+
+ function getCenterCoordinatesAndSizesLayer(x) {
+ var vec = unstack(transpose(x, [1, 0]));
+ var sizes = [
+ sub(vec[2], vec[0]),
+ sub(vec[3], vec[1])
+ ];
+ var centers = [
+ add(vec[0], div(sizes[0], scalar(2))),
+ add(vec[1], div(sizes[1], scalar(2)))
+ ];
+ return {
+ sizes: sizes,
+ centers: centers
};
- prng.int32 = xg.next;
- prng.quick = prng;
- if (state) {
- if (typeof(state) == 'object') copy(state, xg);
- prng.state = function() { return copy(xg, {}); };
+ }
+ function decodeBoxesLayer(x0, x1) {
+ var _a = getCenterCoordinatesAndSizesLayer(x0), sizes = _a.sizes, centers = _a.centers;
+ var vec = unstack(transpose(x1, [1, 0]));
+ var div0_out = div(mul(exp(div(vec[2], scalar(5))), sizes[0]), scalar(2));
+ var add0_out = add(mul(div(vec[0], scalar(10)), sizes[0]), centers[0]);
+ var div1_out = div(mul(exp(div(vec[3], scalar(5))), sizes[1]), scalar(2));
+ var add1_out = add(mul(div(vec[1], scalar(10)), sizes[1]), centers[1]);
+ return transpose(stack([
+ sub(add0_out, div0_out),
+ sub(add1_out, div1_out),
+ add(add0_out, div0_out),
+ add(add1_out, div1_out)
+ ]), [1, 0]);
+ }
+ function outputLayer(boxPredictions, classPredictions, params) {
+ return tidy(function () {
+ var batchSize = boxPredictions.shape[0];
+ var boxes = decodeBoxesLayer(reshape(tile(params.extra_dim, [batchSize, 1, 1]), [-1, 4]), reshape(boxPredictions, [-1, 4]));
+ boxes = reshape(boxes, [batchSize, (boxes.shape[0] / batchSize), 4]);
+ var scoresAndClasses = sigmoid(slice(classPredictions, [0, 0, 1], [-1, -1, -1]));
+ var scores = slice(scoresAndClasses, [0, 0, 0], [-1, -1, 1]);
+ scores = reshape(scores, [batchSize, scores.shape[1]]);
+ var boxesByBatch = unstack(boxes);
+ var scoresByBatch = unstack(scores);
+ return {
+ boxes: boxesByBatch,
+ scores: scoresByBatch
+ };
+ });
+ }
+
+ function convLayer(x, params, padding, withRelu) {
+ if (padding === void 0) { padding = 'same'; }
+ if (withRelu === void 0) { withRelu = false; }
+ return tidy(function () {
+ var out = add(conv2d(x, params.filters, [1, 1], padding), params.bias);
+ return withRelu ? relu(out) : out;
+ });
+ }
+
+ function boxPredictionLayer(x, params) {
+ return tidy(function () {
+ var batchSize = x.shape[0];
+ var boxPredictionEncoding = reshape(convLayer(x, params.box_encoding_predictor_params), [batchSize, -1, 1, 4]);
+ var classPrediction = reshape(convLayer(x, params.class_predictor_params), [batchSize, -1, 3]);
+ return {
+ boxPredictionEncoding: boxPredictionEncoding,
+ classPrediction: classPrediction
+ };
+ });
+ }
+
+ function predictionLayer(x, conv11, params) {
+ return tidy(function () {
+ var conv0 = pointwiseConvLayer(x, params.conv_0_params, [1, 1]);
+ var conv1 = pointwiseConvLayer(conv0, params.conv_1_params, [2, 2]);
+ var conv2 = pointwiseConvLayer(conv1, params.conv_2_params, [1, 1]);
+ var conv3 = pointwiseConvLayer(conv2, params.conv_3_params, [2, 2]);
+ var conv4 = pointwiseConvLayer(conv3, params.conv_4_params, [1, 1]);
+ var conv5 = pointwiseConvLayer(conv4, params.conv_5_params, [2, 2]);
+ var conv6 = pointwiseConvLayer(conv5, params.conv_6_params, [1, 1]);
+ var conv7 = pointwiseConvLayer(conv6, params.conv_7_params, [2, 2]);
+ var boxPrediction0 = boxPredictionLayer(conv11, params.box_predictor_0_params);
+ var boxPrediction1 = boxPredictionLayer(x, params.box_predictor_1_params);
+ var boxPrediction2 = boxPredictionLayer(conv1, params.box_predictor_2_params);
+ var boxPrediction3 = boxPredictionLayer(conv3, params.box_predictor_3_params);
+ var boxPrediction4 = boxPredictionLayer(conv5, params.box_predictor_4_params);
+ var boxPrediction5 = boxPredictionLayer(conv7, params.box_predictor_5_params);
+ var boxPredictions = concat([
+ boxPrediction0.boxPredictionEncoding,
+ boxPrediction1.boxPredictionEncoding,
+ boxPrediction2.boxPredictionEncoding,
+ boxPrediction3.boxPredictionEncoding,
+ boxPrediction4.boxPredictionEncoding,
+ boxPrediction5.boxPredictionEncoding
+ ], 1);
+ var classPredictions = concat([
+ boxPrediction0.classPrediction,
+ boxPrediction1.classPrediction,
+ boxPrediction2.classPrediction,
+ boxPrediction3.classPrediction,
+ boxPrediction4.classPrediction,
+ boxPrediction5.classPrediction
+ ], 1);
+ return {
+ boxPredictions: boxPredictions,
+ classPredictions: classPredictions
+ };
+ });
+ }
+
+ var resizedImageSize = [512, 512];
+ var weight = scalar(0.007843137718737125);
+ var bias = scalar(1);
+ function resizeLayer(x) {
+ return tidy(function () {
+ var resized = image.resizeBilinear(x, resizedImageSize, false);
+ return sub(mul(resized, weight), bias);
+ });
+ }
+
+ var FaceDetectionNet = /** @class */ (function () {
+ function FaceDetectionNet() {
}
- return prng;
- }
-
- if (module && module.exports) {
- module.exports = impl;
- } else if (define && define.amd) {
- define(function() { return impl; });
- } else {
- this.xorwow = impl;
- }
-
- })(
- commonjsGlobal,
- module, // present in node.js
- (typeof undefined) == 'function' && undefined // present with an AMD loader
- );
- });
-
- var xorshift7 = createCommonjsModule(function (module) {
- // A Javascript implementaion of the "xorshift7" algorithm by
- // François Panneton and Pierre L'ecuyer:
- // "On the Xorgshift Random Number Generators"
- // http://saluc.engr.uconn.edu/refs/crypto/rng/panneton05onthexorshift.pdf
-
- (function(global, module, define) {
-
- function XorGen(seed) {
- var me = this;
-
- // Set up generator function.
- me.next = function() {
- // Update xor generator.
- var X = me.x, i = me.i, t, v;
- t = X[i]; t ^= (t >>> 7); v = t ^ (t << 24);
- t = X[(i + 1) & 7]; v ^= t ^ (t >>> 10);
- t = X[(i + 3) & 7]; v ^= t ^ (t >>> 3);
- t = X[(i + 4) & 7]; v ^= t ^ (t << 7);
- t = X[(i + 7) & 7]; t = t ^ (t << 13); v ^= t ^ (t << 9);
- X[i] = v;
- me.i = (i + 1) & 7;
- return v;
+ FaceDetectionNet.prototype.load = function (weightsOrUrl) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var _a;
+ return __generator$1(this, function (_b) {
+ switch (_b.label) {
+ case 0:
+ if (weightsOrUrl instanceof Float32Array) {
+ this.extractWeights(weightsOrUrl);
+ return [2 /*return*/];
+ }
+ if (weightsOrUrl && typeof weightsOrUrl !== 'string') {
+ throw new Error('FaceDetectionNet.load - expected model uri, or weights as Float32Array');
+ }
+ _a = this;
+ return [4 /*yield*/, loadQuantizedParams(weightsOrUrl)];
+ case 1:
+ _a._params = _b.sent();
+ return [2 /*return*/];
+ }
+ });
+ });
};
-
- function init(me, seed) {
- var j, w, X = [];
-
- if (seed === (seed | 0)) {
- // Seed state array using a 32-bit integer.
- w = X[0] = seed;
- } else {
- // Seed state using a string.
- seed = '' + seed;
- for (j = 0; j < seed.length; ++j) {
- X[j & 7] = (X[j & 7] << 15) ^
- (seed.charCodeAt(j) + X[(j + 1) & 7] << 13);
+ FaceDetectionNet.prototype.extractWeights = function (weights) {
+ this._params = extractParams(weights);
+ };
+ FaceDetectionNet.prototype.forwardTensor = function (imgTensor) {
+ var _this = this;
+ if (!this._params) {
+ throw new Error('FaceDetectionNet - load model before inference');
}
- }
- // Enforce an array length of 8, not all zeroes.
- while (X.length < 8) X.push(0);
- for (j = 0; j < 8 && X[j] === 0; ++j);
- if (j == 8) w = X[7] = -1; else w = X[j];
-
- me.x = X;
- me.i = 0;
-
- // Discard an initial 256 values.
- for (j = 256; j > 0; --j) {
- me.next();
- }
+ return tidy(function () {
+ var resized = resizeLayer(imgTensor);
+ var features = mobileNetV1(resized, _this._params.mobilenetv1_params);
+ var _a = predictionLayer(features.out, features.conv11, _this._params.prediction_layer_params), boxPredictions = _a.boxPredictions, classPredictions = _a.classPredictions;
+ return outputLayer(boxPredictions, classPredictions, _this._params.output_layer_params);
+ });
+ };
+ FaceDetectionNet.prototype.forward = function (input) {
+ var _this = this;
+ return tidy(function () { return _this.forwardTensor(padToSquare(getImageTensor(input))); });
+ };
+ FaceDetectionNet.prototype.locateFaces = function (input, minConfidence, maxResults) {
+ if (minConfidence === void 0) { minConfidence = 0.8; }
+ if (maxResults === void 0) { maxResults = 100; }
+ return __awaiter$1(this, void 0, void 0, function () {
+ var _this = this;
+ var paddedHeightRelative, paddedWidthRelative, imageDimensions, _a, _boxes, _scores, boxes, scores, i, scoresData, _b, _c, iouThreshold, indices, results;
+ return __generator$1(this, function (_d) {
+ switch (_d.label) {
+ case 0:
+ paddedHeightRelative = 1, paddedWidthRelative = 1;
+ _a = tidy(function () {
+ var imgTensor = getImageTensor(input);
+ var _a = imgTensor.shape.slice(1), height = _a[0], width = _a[1];
+ imageDimensions = { width: width, height: height };
+ imgTensor = padToSquare(imgTensor);
+ paddedHeightRelative = imgTensor.shape[1] / height;
+ paddedWidthRelative = imgTensor.shape[2] / width;
+ return _this.forwardTensor(imgTensor);
+ }), _boxes = _a.boxes, _scores = _a.scores;
+ boxes = _boxes[0];
+ scores = _scores[0];
+ for (i = 1; i < _boxes.length; i++) {
+ _boxes[i].dispose();
+ _scores[i].dispose();
+ }
+ _c = (_b = Array).from;
+ return [4 /*yield*/, scores.data()];
+ case 1:
+ scoresData = _c.apply(_b, [_d.sent()]);
+ iouThreshold = 0.5;
+ indices = nonMaxSuppression(boxes, scoresData, maxResults, iouThreshold, minConfidence);
+ results = indices
+ .map(function (idx) {
+ var _a = [
+ Math.max(0, boxes.get(idx, 0)),
+ Math.min(1.0, boxes.get(idx, 2))
+ ].map(function (val) { return val * paddedHeightRelative; }), top = _a[0], bottom = _a[1];
+ var _b = [
+ Math.max(0, boxes.get(idx, 1)),
+ Math.min(1.0, boxes.get(idx, 3))
+ ].map(function (val) { return val * paddedWidthRelative; }), left = _b[0], right = _b[1];
+ return new FaceDetection(scoresData[idx], new Rect(left, top, right - left, bottom - top), imageDimensions);
+ });
+ boxes.dispose();
+ scores.dispose();
+ return [2 /*return*/, results];
+ }
+ });
+ });
+ };
+ return FaceDetectionNet;
+ }());
+
+ function faceDetectionNet(weights) {
+ var net = new FaceDetectionNet();
+ net.extractWeights(weights);
+ return net;
+ }
+
+ var Point = /** @class */ (function () {
+ function Point(x, y) {
+ this.x = x;
+ this.y = y;
}
-
- init(me, seed);
- }
-
- function copy(f, t) {
- t.x = f.x.slice();
- t.i = f.i;
- return t;
- }
-
- function impl(seed, opts) {
- if (seed == null) seed = +(new Date);
- var xg = new XorGen(seed),
- state = opts && opts.state,
- prng = function() { return (xg.next() >>> 0) / 0x100000000; };
- prng.double = function() {
- do {
- var top = xg.next() >>> 11,
- bot = (xg.next() >>> 0) / 0x100000000,
- result = (top + bot) / (1 << 21);
- } while (result === 0);
- return result;
+ Point.prototype.add = function (pt) {
+ return new Point(this.x + pt.x, this.y + pt.y);
+ };
+ Point.prototype.sub = function (pt) {
+ return new Point(this.x - pt.x, this.y - pt.y);
+ };
+ Point.prototype.mul = function (pt) {
+ return new Point(this.x * pt.x, this.y * pt.y);
+ };
+ Point.prototype.div = function (pt) {
+ return new Point(this.x / pt.x, this.y / pt.y);
};
- prng.int32 = xg.next;
- prng.quick = prng;
- if (state) {
- if (state.x) copy(state, xg);
- prng.state = function() { return copy(xg, {}); };
+ Point.prototype.abs = function () {
+ return new Point(Math.abs(this.x), Math.abs(this.y));
+ };
+ Point.prototype.magnitude = function () {
+ return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
+ };
+ Point.prototype.floor = function () {
+ return new Point(Math.floor(this.x), Math.floor(this.y));
+ };
+ return Point;
+ }());
+
+ function extractConvParamsFactory(extractWeights) {
+ return function (channelsIn, channelsOut, filterSize) {
+ var filters = tensor4d(extractWeights(channelsIn * channelsOut * filterSize * filterSize), [filterSize, filterSize, channelsIn, channelsOut]);
+ var bias = tensor1d(extractWeights(channelsOut));
+ return {
+ filters: filters,
+ bias: bias
+ };
+ };
+ }
+
+ function extractParams$1(weights) {
+ var _a = extractWeightsFactory(weights), extractWeights = _a.extractWeights, getRemainingWeights = _a.getRemainingWeights;
+ var extractConvParams = extractConvParamsFactory(extractWeights);
+ function extractFcParams(channelsIn, channelsOut) {
+ var fc_weights = tensor2d(extractWeights(channelsIn * channelsOut), [channelsIn, channelsOut]);
+ var fc_bias = tensor1d(extractWeights(channelsOut));
+ return {
+ weights: fc_weights,
+ bias: fc_bias
+ };
}
- return prng;
- }
-
- if (module && module.exports) {
- module.exports = impl;
- } else if (define && define.amd) {
- define(function() { return impl; });
- } else {
- this.xorshift7 = impl;
- }
-
- })(
- commonjsGlobal,
- module, // present in node.js
- (typeof undefined) == 'function' && undefined // present with an AMD loader
- );
- });
-
- var xor4096 = createCommonjsModule(function (module) {
- // A Javascript implementaion of Richard Brent's Xorgens xor4096 algorithm.
- //
- // This fast non-cryptographic random number generator is designed for
- // use in Monte-Carlo algorithms. It combines a long-period xorshift
- // generator with a Weyl generator, and it passes all common batteries
- // of stasticial tests for randomness while consuming only a few nanoseconds
- // for each prng generated. For background on the generator, see Brent's
- // paper: "Some long-period random number generators using shifts and xors."
- // http://arxiv.org/pdf/1004.3115v1.pdf
- //
- // Usage:
- //
- // var xor4096 = require('xor4096');
- // random = xor4096(1); // Seed with int32 or string.
- // assert.equal(random(), 0.1520436450538547); // (0, 1) range, 53 bits.
- // assert.equal(random.int32(), 1806534897); // signed int32, 32 bits.
- //
- // For nonzero numeric keys, this impelementation provides a sequence
- // identical to that by Brent's xorgens 3 implementaion in C. This
- // implementation also provides for initalizing the generator with
- // string seeds, or for saving and restoring the state of the generator.
- //
- // On Chrome, this prng benchmarks about 2.1 times slower than
- // Javascript's built-in Math.random().
-
- (function(global, module, define) {
-
- function XorGen(seed) {
- var me = this;
-
- // Set up generator function.
- me.next = function() {
- var w = me.w,
- X = me.X, i = me.i, t, v;
- // Update Weyl generator.
- me.w = w = (w + 0x61c88647) | 0;
- // Update xor generator.
- v = X[(i + 34) & 127];
- t = X[i = ((i + 1) & 127)];
- v ^= v << 13;
- t ^= t << 17;
- v ^= v >>> 15;
- t ^= t >>> 12;
- // Update Xor generator array state.
- v = X[i] = v ^ t;
- me.i = i;
- // Result is the combination.
- return (v + (w ^ (w >>> 16))) | 0;
+ var conv0_params = extractConvParams(3, 32, 3);
+ var conv1_params = extractConvParams(32, 64, 3);
+ var conv2_params = extractConvParams(64, 64, 3);
+ var conv3_params = extractConvParams(64, 64, 3);
+ var conv4_params = extractConvParams(64, 64, 3);
+ var conv5_params = extractConvParams(64, 128, 3);
+ var conv6_params = extractConvParams(128, 128, 3);
+ var conv7_params = extractConvParams(128, 256, 3);
+ var fc0_params = extractFcParams(6400, 1024);
+ var fc1_params = extractFcParams(1024, 136);
+ if (getRemainingWeights().length !== 0) {
+ throw new Error("weights remaing after extract: " + getRemainingWeights().length);
+ }
+ return {
+ conv0_params: conv0_params,
+ conv1_params: conv1_params,
+ conv2_params: conv2_params,
+ conv3_params: conv3_params,
+ conv4_params: conv4_params,
+ conv5_params: conv5_params,
+ conv6_params: conv6_params,
+ conv7_params: conv7_params,
+ fc0_params: fc0_params,
+ fc1_params: fc1_params
};
-
- function init(me, seed) {
- var t, v, i, j, w, X = [], limit = 128;
- if (seed === (seed | 0)) {
- // Numeric seeds initialize v, which is used to generates X.
- v = seed;
- seed = null;
- } else {
- // String seeds are mixed into v and X one character at a time.
- seed = seed + '\0';
- v = 0;
- limit = Math.max(limit, seed.length);
- }
- // Initialize circular array and weyl value.
- for (i = 0, j = -32; j < limit; ++j) {
- // Put the unicode characters into the array, and shuffle them.
- if (seed) v ^= seed.charCodeAt((j + 32) % seed.length);
- // After 32 shuffles, take v as the starting w value.
- if (j === 0) w = v;
- v ^= v << 10;
- v ^= v >>> 15;
- v ^= v << 4;
- v ^= v >>> 13;
- if (j >= 0) {
- w = (w + 0x61c88647) | 0; // Weyl.
- t = (X[j & 127] ^= (v + w)); // Combine xor and weyl to init array.
- i = (0 == t) ? i + 1 : 0; // Count zeroes.
+ }
+
+ function getCenterPoint(pts) {
+ return pts.reduce(function (sum, pt) { return sum.add(pt); }, new Point(0, 0))
+ .div(new Point(pts.length, pts.length));
+ }
+
+ // face alignment constants
+ var relX = 0.5;
+ var relY = 0.43;
+ var relScale = 0.45;
+ var FaceLandmarks = /** @class */ (function () {
+ function FaceLandmarks(relativeFaceLandmarkPositions, imageDims, shift) {
+ if (shift === void 0) { shift = new Point(0, 0); }
+ var width = imageDims.width, height = imageDims.height;
+ this._imageWidth = width;
+ this._imageHeight = height;
+ this._shift = shift;
+ this._faceLandmarks = relativeFaceLandmarkPositions.map(function (pt) { return pt.mul(new Point(width, height)).add(shift); });
+ }
+ FaceLandmarks.prototype.getShift = function () {
+ return new Point(this._shift.x, this._shift.y);
+ };
+ FaceLandmarks.prototype.getImageWidth = function () {
+ return this._imageWidth;
+ };
+ FaceLandmarks.prototype.getImageHeight = function () {
+ return this._imageHeight;
+ };
+ FaceLandmarks.prototype.getPositions = function () {
+ return this._faceLandmarks;
+ };
+ FaceLandmarks.prototype.getRelativePositions = function () {
+ var _this = this;
+ return this._faceLandmarks.map(function (pt) { return pt.sub(_this._shift).div(new Point(_this._imageWidth, _this._imageHeight)); });
+ };
+ FaceLandmarks.prototype.getJawOutline = function () {
+ return this._faceLandmarks.slice(0, 17);
+ };
+ FaceLandmarks.prototype.getLeftEyeBrow = function () {
+ return this._faceLandmarks.slice(17, 22);
+ };
+ FaceLandmarks.prototype.getRightEyeBrow = function () {
+ return this._faceLandmarks.slice(22, 27);
+ };
+ FaceLandmarks.prototype.getNose = function () {
+ return this._faceLandmarks.slice(27, 36);
+ };
+ FaceLandmarks.prototype.getLeftEye = function () {
+ return this._faceLandmarks.slice(36, 42);
+ };
+ FaceLandmarks.prototype.getRightEye = function () {
+ return this._faceLandmarks.slice(42, 48);
+ };
+ FaceLandmarks.prototype.getMouth = function () {
+ return this._faceLandmarks.slice(48, 68);
+ };
+ FaceLandmarks.prototype.forSize = function (width, height) {
+ return new FaceLandmarks(this.getRelativePositions(), { width: width, height: height });
+ };
+ FaceLandmarks.prototype.shift = function (x, y) {
+ return new FaceLandmarks(this.getRelativePositions(), { width: this._imageWidth, height: this._imageHeight }, new Point(x, y));
+ };
+ /**
+ * Aligns the face landmarks after face detection from the relative positions of the faces
+ * bounding box, or it's current shift. This function should be used to align the face images
+ * after face detection has been performed, before they are passed to the face recognition net.
+ * This will make the computed face descriptor more accurate.
+ *
+ * @param detection (optional) The bounding box of the face or the face detection result. If
+ * no argument was passed the position of the face landmarks are assumed to be relative to
+ * it's current shift.
+ * @returns The bounding box of the aligned face.
+ */
+ FaceLandmarks.prototype.align = function (detection) {
+ if (detection) {
+ var box = detection instanceof FaceDetection
+ ? detection.getBox().floor()
+ : detection;
+ return this.shift(box.x, box.y).align();
+ }
+ var centers = [
+ this.getLeftEye(),
+ this.getRightEye(),
+ this.getMouth()
+ ].map(getCenterPoint);
+ var leftEyeCenter = centers[0], rightEyeCenter = centers[1], mouthCenter = centers[2];
+ var distToMouth = function (pt) { return mouthCenter.sub(pt).magnitude(); };
+ var eyeToMouthDist = (distToMouth(leftEyeCenter) + distToMouth(rightEyeCenter)) / 2;
+ var size = Math.floor(eyeToMouthDist / relScale);
+ var refPoint = getCenterPoint(centers);
+ // TODO: pad in case rectangle is out of image bounds
+ var x = Math.floor(Math.max(0, refPoint.x - (relX * size)));
+ var y = Math.floor(Math.max(0, refPoint.y - (relY * size)));
+ return new Rect(x, y, size, size);
+ };
+ return FaceLandmarks;
+ }());
+
+ function fullyConnectedLayer(x, params) {
+ return tidy(function () {
+ return add(matMul(x, params.weights), params.bias);
+ });
+ }
+
+ var DEFAULT_MODEL_NAME$1 = 'face_landmark_68_model';
+ function extractorsFactory$2(weightMap) {
+ function extractConvParams(prefix) {
+ var params = {
+ filters: weightMap[prefix + "/kernel"],
+ bias: weightMap[prefix + "/bias"]
+ };
+ if (!isTensor4D(params.filters)) {
+ throw new Error("expected weightMap[" + prefix + "/kernel] to be a Tensor4D, instead have " + params.filters);
+ }
+ if (!isTensor1D(params.bias)) {
+ throw new Error("expected weightMap[" + prefix + "/bias] to be a Tensor1D, instead have " + params.bias);
}
- }
- // We have detected all zeroes; make the key nonzero.
- if (i >= 128) {
- X[(seed && seed.length || 0) & 127] = -1;
- }
- // Run the generator 512 times to further mix the state before using it.
- // Factoring this as a function slows the main generator, so it is just
- // unrolled here. The weyl generator is not advanced while warming up.
- i = 127;
- for (j = 4 * 128; j > 0; --j) {
- v = X[(i + 34) & 127];
- t = X[i = ((i + 1) & 127)];
- v ^= v << 13;
- t ^= t << 17;
- v ^= v >>> 15;
- t ^= t >>> 12;
- X[i] = v ^ t;
- }
- // Storing state as object members is faster than using closure variables.
- me.w = w;
- me.X = X;
- me.i = i;
+ return params;
}
-
- init(me, seed);
- }
-
- function copy(f, t) {
- t.i = f.i;
- t.w = f.w;
- t.X = f.X.slice();
- return t;
- }
- function impl(seed, opts) {
- if (seed == null) seed = +(new Date);
- var xg = new XorGen(seed),
- state = opts && opts.state,
- prng = function() { return (xg.next() >>> 0) / 0x100000000; };
- prng.double = function() {
- do {
- var top = xg.next() >>> 11,
- bot = (xg.next() >>> 0) / 0x100000000,
- result = (top + bot) / (1 << 21);
- } while (result === 0);
- return result;
+ function extractFcParams(prefix) {
+ var params = {
+ weights: weightMap[prefix + "/kernel"],
+ bias: weightMap[prefix + "/bias"]
+ };
+ if (!isTensor2D(params.weights)) {
+ throw new Error("expected weightMap[" + prefix + "/kernel] to be a Tensor2D, instead have " + params.weights);
+ }
+ if (!isTensor1D(params.bias)) {
+ throw new Error("expected weightMap[" + prefix + "/bias] to be a Tensor1D, instead have " + params.bias);
+ }
+ return params;
+ }
+ return {
+ extractConvParams: extractConvParams,
+ extractFcParams: extractFcParams
};
- prng.int32 = xg.next;
- prng.quick = prng;
- if (state) {
- if (state.X) copy(state, xg);
- prng.state = function() { return copy(xg, {}); };
+ }
+ function loadQuantizedParams$1(uri) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var weightMap, _a, extractConvParams, extractFcParams;
+ return __generator$1(this, function (_b) {
+ switch (_b.label) {
+ case 0: return [4 /*yield*/, loadWeightMap(uri, DEFAULT_MODEL_NAME$1)];
+ case 1:
+ weightMap = _b.sent();
+ _a = extractorsFactory$2(weightMap), extractConvParams = _a.extractConvParams, extractFcParams = _a.extractFcParams;
+ return [2 /*return*/, {
+ conv0_params: extractConvParams('conv2d_0'),
+ conv1_params: extractConvParams('conv2d_1'),
+ conv2_params: extractConvParams('conv2d_2'),
+ conv3_params: extractConvParams('conv2d_3'),
+ conv4_params: extractConvParams('conv2d_4'),
+ conv5_params: extractConvParams('conv2d_5'),
+ conv6_params: extractConvParams('conv2d_6'),
+ conv7_params: extractConvParams('conv2d_7'),
+ fc0_params: extractFcParams('dense'),
+ fc1_params: extractFcParams('logits')
+ }];
+ }
+ });
+ });
+ }
+
+ function conv(x, params) {
+ return convLayer(x, params, 'valid', true);
+ }
+ function maxPool$1(x, strides) {
+ if (strides === void 0) { strides = [2, 2]; }
+ return maxPool(x, [2, 2], strides, 'valid');
+ }
+ var FaceLandmarkNet = /** @class */ (function () {
+ function FaceLandmarkNet() {
}
- return prng;
- }
-
- if (module && module.exports) {
- module.exports = impl;
- } else if (define && define.amd) {
- define(function() { return impl; });
- } else {
- this.xor4096 = impl;
- }
-
- })(
- commonjsGlobal, // window object or global
- module, // present in node.js
- (typeof undefined) == 'function' && undefined // present with an AMD loader
- );
- });
-
- var tychei = createCommonjsModule(function (module) {
- // A Javascript implementaion of the "Tyche-i" prng algorithm by
- // Samuel Neves and Filipe Araujo.
- // See https://eden.dei.uc.pt/~sneves/pubs/2011-snfa2.pdf
-
- (function(global, module, define) {
-
- function XorGen(seed) {
- var me = this, strseed = '';
-
- // Set up generator function.
- me.next = function() {
- var b = me.b, c = me.c, d = me.d, a = me.a;
- b = (b << 25) ^ (b >>> 7) ^ c;
- c = (c - d) | 0;
- d = (d << 24) ^ (d >>> 8) ^ a;
- a = (a - b) | 0;
- me.b = b = (b << 20) ^ (b >>> 12) ^ c;
- me.c = c = (c - d) | 0;
- me.d = (d << 16) ^ (c >>> 16) ^ a;
- return me.a = (a - b) | 0;
+ FaceLandmarkNet.prototype.load = function (weightsOrUrl) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var _a;
+ return __generator$1(this, function (_b) {
+ switch (_b.label) {
+ case 0:
+ if (weightsOrUrl instanceof Float32Array) {
+ this.extractWeights(weightsOrUrl);
+ return [2 /*return*/];
+ }
+ if (weightsOrUrl && typeof weightsOrUrl !== 'string') {
+ throw new Error('FaceLandmarkNet.load - expected model uri, or weights as Float32Array');
+ }
+ _a = this;
+ return [4 /*yield*/, loadQuantizedParams$1(weightsOrUrl)];
+ case 1:
+ _a._params = _b.sent();
+ return [2 /*return*/];
+ }
+ });
+ });
};
-
- /* The following is non-inverted tyche, which has better internal
- * bit diffusion, but which is about 25% slower than tyche-i in JS.
- me.next = function() {
- var a = me.a, b = me.b, c = me.c, d = me.d;
- a = (me.a + me.b | 0) >>> 0;
- d = me.d ^ a; d = d << 16 ^ d >>> 16;
- c = me.c + d | 0;
- b = me.b ^ c; b = b << 12 ^ d >>> 20;
- me.a = a = a + b | 0;
- d = d ^ a; me.d = d = d << 8 ^ d >>> 24;
- me.c = c = c + d | 0;
- b = b ^ c;
- return me.b = (b << 7 ^ b >>> 25);
+ FaceLandmarkNet.prototype.extractWeights = function (weights) {
+ this._params = extractParams$1(weights);
+ };
+ FaceLandmarkNet.prototype.detectLandmarks = function (input) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var _this = this;
+ var imageDimensions, outTensor, faceLandmarksArray, _a, _b, xCoords, yCoords;
+ return __generator$1(this, function (_c) {
+ switch (_c.label) {
+ case 0:
+ if (!this._params) {
+ throw new Error('FaceLandmarkNet - load model before inference');
+ }
+ outTensor = tidy(function () {
+ var params = _this._params;
+ var imgTensor = getImageTensor(input);
+ var _a = imgTensor.shape.slice(1), height = _a[0], width = _a[1];
+ imageDimensions = { width: width, height: height };
+ // work with 128 x 128 sized face images
+ if (imgTensor.shape[1] !== 128 || imgTensor.shape[2] !== 128) {
+ imgTensor = image.resizeBilinear(imgTensor, [128, 128]);
+ }
+ var out = conv(imgTensor, params.conv0_params);
+ out = maxPool$1(out);
+ out = conv(out, params.conv1_params);
+ out = conv(out, params.conv2_params);
+ out = maxPool$1(out);
+ out = conv(out, params.conv3_params);
+ out = conv(out, params.conv4_params);
+ out = maxPool$1(out);
+ out = conv(out, params.conv5_params);
+ out = conv(out, params.conv6_params);
+ out = maxPool$1(out, [1, 1]);
+ out = conv(out, params.conv7_params);
+ var fc0 = relu(fullyConnectedLayer(out.as2D(out.shape[0], -1), params.fc0_params));
+ var fc1 = fullyConnectedLayer(fc0, params.fc1_params);
+ return fc1;
+ });
+ _b = (_a = Array).from;
+ return [4 /*yield*/, outTensor.data()];
+ case 1:
+ faceLandmarksArray = _b.apply(_a, [_c.sent()]);
+ outTensor.dispose();
+ xCoords = faceLandmarksArray.filter(function (c, i) { return (i - 1) % 2; });
+ yCoords = faceLandmarksArray.filter(function (c, i) { return i % 2; });
+ return [2 /*return*/, new FaceLandmarks(Array(68).fill(0).map(function (_, i) { return new Point(xCoords[i], yCoords[i]); }), imageDimensions)];
+ }
+ });
+ });
+ };
+ return FaceLandmarkNet;
+ }());
+
+ function faceLandmarkNet(weights) {
+ var net = new FaceLandmarkNet();
+ net.extractWeights(weights);
+ return net;
+ }
+
+ function scale(x, params) {
+ return add(mul(x, params.weights), params.biases);
+ }
+
+ function convLayer$1(x, params, strides, withRelu, padding) {
+ if (padding === void 0) { padding = 'same'; }
+ var _a = params.conv, filters = _a.filters, bias = _a.bias;
+ var out = conv2d(x, filters, strides, padding);
+ out = add(out, bias);
+ out = scale(out, params.scale);
+ return withRelu ? relu(out) : out;
+ }
+ function conv$1(x, params) {
+ return convLayer$1(x, params, [1, 1], true);
+ }
+ function convNoRelu(x, params) {
+ return convLayer$1(x, params, [1, 1], false);
+ }
+ function convDown(x, params) {
+ return convLayer$1(x, params, [2, 2], true, 'valid');
+ }
+
+ function extractorsFactory$3(extractWeights) {
+ function extractFilterValues(numFilterValues, numFilters, filterSize) {
+ var weights = extractWeights(numFilterValues);
+ var depth = weights.length / (numFilters * filterSize * filterSize);
+ if (isFloat(depth)) {
+ throw new Error("depth has to be an integer: " + depth + ", weights.length: " + weights.length + ", numFilters: " + numFilters + ", filterSize: " + filterSize);
+ }
+ return transpose(tensor4d(weights, [numFilters, depth, filterSize, filterSize]), [2, 3, 1, 0]);
}
- */
-
- me.a = 0;
- me.b = 0;
- me.c = 2654435769 | 0;
- me.d = 1367130551;
-
- if (seed === Math.floor(seed)) {
- // Integer seed.
- me.a = (seed / 0x100000000) | 0;
- me.b = seed | 0;
- } else {
- // String seed.
- strseed += seed;
+ function extractScaleLayerParams(numWeights) {
+ var weights = tensor1d(extractWeights(numWeights));
+ var biases = tensor1d(extractWeights(numWeights));
+ return {
+ weights: weights,
+ biases: biases
+ };
}
-
- // Mix in string seed, then discard an initial batch of 64 values.
- for (var k = 0; k < strseed.length + 20; k++) {
- me.b ^= strseed.charCodeAt(k) | 0;
- me.next();
+ function extractConvLayerParams(numFilterValues, numFilters, filterSize) {
+ var conv_filters = extractFilterValues(numFilterValues, numFilters, filterSize);
+ var conv_bias = tensor1d(extractWeights(numFilters));
+ var scale = extractScaleLayerParams(numFilters);
+ return {
+ conv: {
+ filters: conv_filters,
+ bias: conv_bias
+ },
+ scale: scale
+ };
}
- }
-
- function copy(f, t) {
- t.a = f.a;
- t.b = f.b;
- t.c = f.c;
- t.d = f.d;
- return t;
- }
- function impl(seed, opts) {
- var xg = new XorGen(seed),
- state = opts && opts.state,
- prng = function() { return (xg.next() >>> 0) / 0x100000000; };
- prng.double = function() {
- do {
- var top = xg.next() >>> 11,
- bot = (xg.next() >>> 0) / 0x100000000,
- result = (top + bot) / (1 << 21);
- } while (result === 0);
- return result;
+ function extractResidualLayerParams(numFilterValues, numFilters, filterSize, isDown) {
+ if (isDown === void 0) { isDown = false; }
+ var conv1 = extractConvLayerParams((isDown ? 0.5 : 1) * numFilterValues, numFilters, filterSize);
+ var conv2 = extractConvLayerParams(numFilterValues, numFilters, filterSize);
+ return {
+ conv1: conv1,
+ conv2: conv2
+ };
+ }
+ return {
+ extractConvLayerParams: extractConvLayerParams,
+ extractResidualLayerParams: extractResidualLayerParams
};
- prng.int32 = xg.next;
- prng.quick = prng;
- if (state) {
- if (typeof(state) == 'object') copy(state, xg);
- prng.state = function() { return copy(xg, {}); };
+ }
+ function extractParams$2(weights) {
+ var _a = extractWeightsFactory(weights), extractWeights = _a.extractWeights, getRemainingWeights = _a.getRemainingWeights;
+ var _b = extractorsFactory$3(extractWeights), extractConvLayerParams = _b.extractConvLayerParams, extractResidualLayerParams = _b.extractResidualLayerParams;
+ var conv32_down = extractConvLayerParams(4704, 32, 7);
+ var conv32_1 = extractResidualLayerParams(9216, 32, 3);
+ var conv32_2 = extractResidualLayerParams(9216, 32, 3);
+ var conv32_3 = extractResidualLayerParams(9216, 32, 3);
+ var conv64_down = extractResidualLayerParams(36864, 64, 3, true);
+ var conv64_1 = extractResidualLayerParams(36864, 64, 3);
+ var conv64_2 = extractResidualLayerParams(36864, 64, 3);
+ var conv64_3 = extractResidualLayerParams(36864, 64, 3);
+ var conv128_down = extractResidualLayerParams(147456, 128, 3, true);
+ var conv128_1 = extractResidualLayerParams(147456, 128, 3);
+ var conv128_2 = extractResidualLayerParams(147456, 128, 3);
+ var conv256_down = extractResidualLayerParams(589824, 256, 3, true);
+ var conv256_1 = extractResidualLayerParams(589824, 256, 3);
+ var conv256_2 = extractResidualLayerParams(589824, 256, 3);
+ var conv256_down_out = extractResidualLayerParams(589824, 256, 3);
+ var fc = transpose(tensor2d(extractWeights(256 * 128), [128, 256]), [1, 0]);
+ if (getRemainingWeights().length !== 0) {
+ throw new Error("weights remaing after extract: " + getRemainingWeights().length);
}
- return prng;
- }
-
- if (module && module.exports) {
- module.exports = impl;
- } else if (define && define.amd) {
- define(function() { return impl; });
- } else {
- this.tychei = impl;
- }
-
- })(
- commonjsGlobal,
- module, // present in node.js
- (typeof undefined) == 'function' && undefined // present with an AMD loader
- );
- });
-
- var seedrandom = createCommonjsModule(function (module) {
- /*
- Copyright 2014 David Bau.
-
- Permission is hereby granted, free of charge, to any person obtaining
- a copy of this software and associated documentation files (the
- "Software"), to deal in the Software without restriction, including
- without limitation the rights to use, copy, modify, merge, publish,
- distribute, sublicense, and/or sell copies of the Software, and to
- permit persons to whom the Software is furnished to do so, subject to
- the following conditions:
-
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
- */
-
- (function (pool, math) {
- //
- // The following constants are related to IEEE 754 limits.
- //
- var global = this,
- width = 256, // each RC4 output is 0 <= x < 256
- chunks = 6, // at least six RC4 outputs for each double
- digits = 52, // there are 52 significant digits in a double
- rngname = 'random', // rngname: name for Math.random and Math.seedrandom
- startdenom = math.pow(width, chunks),
- significance = math.pow(2, digits),
- overflow = significance * 2,
- mask = width - 1,
- nodecrypto; // node.js crypto module, initialized at the bottom.
-
- //
- // seedrandom()
- // This is the seedrandom function described above.
- //
- function seedrandom(seed, options, callback) {
- var key = [];
- options = (options == true) ? { entropy: true } : (options || {});
-
- // Flatten the seed string or build one from local entropy if needed.
- var shortseed = mixkey(flatten(
- options.entropy ? [seed, tostring(pool)] :
- (seed == null) ? autoseed() : seed, 3), key);
-
- // Use the seed to initialize an ARC4 generator.
- var arc4 = new ARC4(key);
-
- // This function returns a random double in [0, 1) that contains
- // randomness in every bit of the mantissa of the IEEE 754 value.
- var prng = function() {
- var n = arc4.g(chunks), // Start with a numerator n < 2 ^ 48
- d = startdenom, // and denominator d = 2 ^ 48.
- x = 0; // and no 'extra last byte'.
- while (n < significance) { // Fill up all significant digits by
- n = (n + x) * width; // shifting numerator and
- d *= width; // denominator and generating a
- x = arc4.g(1); // new least-significant-byte.
- }
- while (n >= overflow) { // To avoid rounding up, before adding
- n /= 2; // last byte, shift everything
- d /= 2; // right using integer math until
- x >>>= 1; // we have exactly the desired bits.
- }
- return (n + x) / d; // Form the number within [0, 1).
+ return {
+ conv32_down: conv32_down,
+ conv32_1: conv32_1,
+ conv32_2: conv32_2,
+ conv32_3: conv32_3,
+ conv64_down: conv64_down,
+ conv64_1: conv64_1,
+ conv64_2: conv64_2,
+ conv64_3: conv64_3,
+ conv128_down: conv128_down,
+ conv128_1: conv128_1,
+ conv128_2: conv128_2,
+ conv256_down: conv256_down,
+ conv256_1: conv256_1,
+ conv256_2: conv256_2,
+ conv256_down_out: conv256_down_out,
+ fc: fc
};
-
- prng.int32 = function() { return arc4.g(4) | 0; };
- prng.quick = function() { return arc4.g(4) / 0x100000000; };
- prng.double = prng;
-
- // Mix the randomness into accumulated entropy.
- mixkey(tostring(arc4.S), pool);
-
- // Calling convention: what to return as a function of prng, seed, is_math.
- return (options.pass || callback ||
- function(prng, seed, is_math_call, state) {
- if (state) {
- // Load the arc4 state from the given state if it has an S array.
- if (state.S) { copy(state, arc4); }
- // Only provide the .state method if requested via options.state.
- prng.state = function() { return copy(arc4, {}); };
- }
-
- // If called as a method of Math (Math.seedrandom()), mutate
- // Math.random because that is how seedrandom.js has worked since v1.0.
- if (is_math_call) { math[rngname] = prng; return seed; }
-
- // Otherwise, it is a newer calling convention, so return the
- // prng directly.
- else return prng;
- })(
- prng,
- shortseed,
- 'global' in options ? options.global : (this == math),
- options.state);
- }
- math['seed' + rngname] = seedrandom;
-
- //
- // ARC4
- //
- // An ARC4 implementation. The constructor takes a key in the form of
- // an array of at most (width) integers that should be 0 <= x < (width).
- //
- // The g(count) method returns a pseudorandom integer that concatenates
- // the next (count) outputs from ARC4. Its return value is a number x
- // that is in the range 0 <= x < (width ^ count).
- //
- function ARC4(key) {
- var t, keylen = key.length,
- me = this, i = 0, j = me.i = me.j = 0, s = me.S = [];
-
- // The empty key [] is treated as [0].
- if (!keylen) { key = [keylen++]; }
-
- // Set up S using the standard key scheduling algorithm.
- while (i < width) {
- s[i] = i++;
+ }
+
+ var DEFAULT_MODEL_NAME$2 = 'face_recognition_model';
+ function extractorsFactory$4(weightMap) {
+ function extractScaleLayerParams(prefix) {
+ var params = {
+ weights: weightMap[prefix + "/scale/weights"],
+ biases: weightMap[prefix + "/scale/biases"]
+ };
+ if (!isTensor1D(params.weights)) {
+ throw new Error("expected weightMap[" + prefix + "/scale/weights] to be a Tensor1D, instead have " + params.weights);
+ }
+ if (!isTensor1D(params.biases)) {
+ throw new Error("expected weightMap[" + prefix + "/scale/biases] to be a Tensor1D, instead have " + params.biases);
+ }
+ return params;
}
- for (i = 0; i < width; i++) {
- s[i] = s[j = mask & (j + key[i % keylen] + (t = s[i]))];
- s[j] = t;
+ function extractConvLayerParams(prefix) {
+ var params = {
+ filters: weightMap[prefix + "/conv/filters"],
+ bias: weightMap[prefix + "/conv/bias"]
+ };
+ if (!isTensor4D(params.filters)) {
+ throw new Error("expected weightMap[" + prefix + "/conv/filters] to be a Tensor1D, instead have " + params.filters);
+ }
+ if (!isTensor1D(params.bias)) {
+ throw new Error("expected weightMap[" + prefix + "/conv/bias] to be a Tensor1D, instead have " + params.bias);
+ }
+ return {
+ conv: params,
+ scale: extractScaleLayerParams(prefix)
+ };
}
-
- // The "g" method returns the next (count) outputs as one number.
- (me.g = function(count) {
- // Using instance members instead of closure state nearly doubles speed.
- var t, r = 0,
- i = me.i, j = me.j, s = me.S;
- while (count--) {
- t = s[i = mask & (i + 1)];
- r = r * width + s[mask & ((s[i] = s[j = mask & (j + t)]) + (s[j] = t))];
- }
- me.i = i; me.j = j;
- return r;
- // For robust unpredictability, the function call below automatically
- // discards an initial batch of values. This is called RC4-drop[256].
- // See http://google.com/search?q=rsa+fluhrer+response&btnI
- })(width);
- }
-
- //
- // copy()
- // Copies internal state of ARC4 to or from a plain object.
- //
- function copy(f, t) {
- t.i = f.i;
- t.j = f.j;
- t.S = f.S.slice();
- return t;
- }
- //
- // flatten()
- // Converts an object tree to nested arrays of strings.
- //
- function flatten(obj, depth) {
- var result = [], typ = (typeof obj), prop;
- if (depth && typ == 'object') {
- for (prop in obj) {
- try { result.push(flatten(obj[prop], depth - 1)); } catch (e) {}
- }
+ function extractResidualLayerParams(prefix) {
+ return {
+ conv1: extractConvLayerParams(prefix + "/conv1"),
+ conv2: extractConvLayerParams(prefix + "/conv2")
+ };
}
- return (result.length ? result : typ == 'string' ? obj : obj + '\0');
- }
-
- //
- // mixkey()
- // Mixes a string seed into a key that is an array of integers, and
- // returns a shortened string seed that is equivalent to the result key.
- //
- function mixkey(seed, key) {
- var stringseed = seed + '', smear, j = 0;
- while (j < stringseed.length) {
- key[mask & j] =
- mask & ((smear ^= key[mask & j] * 19) + stringseed.charCodeAt(j++));
+ return {
+ extractConvLayerParams: extractConvLayerParams,
+ extractResidualLayerParams: extractResidualLayerParams
+ };
+ }
+ function loadQuantizedParams$2(uri) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var weightMap, _a, extractConvLayerParams, extractResidualLayerParams, conv32_down, conv32_1, conv32_2, conv32_3, conv64_down, conv64_1, conv64_2, conv64_3, conv128_down, conv128_1, conv128_2, conv256_down, conv256_1, conv256_2, conv256_down_out, fc;
+ return __generator$1(this, function (_b) {
+ switch (_b.label) {
+ case 0: return [4 /*yield*/, loadWeightMap(uri, DEFAULT_MODEL_NAME$2)];
+ case 1:
+ weightMap = _b.sent();
+ _a = extractorsFactory$4(weightMap), extractConvLayerParams = _a.extractConvLayerParams, extractResidualLayerParams = _a.extractResidualLayerParams;
+ conv32_down = extractConvLayerParams('conv32_down');
+ conv32_1 = extractResidualLayerParams('conv32_1');
+ conv32_2 = extractResidualLayerParams('conv32_2');
+ conv32_3 = extractResidualLayerParams('conv32_3');
+ conv64_down = extractResidualLayerParams('conv64_down');
+ conv64_1 = extractResidualLayerParams('conv64_1');
+ conv64_2 = extractResidualLayerParams('conv64_2');
+ conv64_3 = extractResidualLayerParams('conv64_3');
+ conv128_down = extractResidualLayerParams('conv128_down');
+ conv128_1 = extractResidualLayerParams('conv128_1');
+ conv128_2 = extractResidualLayerParams('conv128_2');
+ conv256_down = extractResidualLayerParams('conv256_down');
+ conv256_1 = extractResidualLayerParams('conv256_1');
+ conv256_2 = extractResidualLayerParams('conv256_2');
+ conv256_down_out = extractResidualLayerParams('conv256_down_out');
+ fc = weightMap['fc'];
+ if (!isTensor2D(fc)) {
+ throw new Error("expected weightMap[fc] to be a Tensor2D, instead have " + fc);
+ }
+ return [2 /*return*/, {
+ conv32_down: conv32_down,
+ conv32_1: conv32_1,
+ conv32_2: conv32_2,
+ conv32_3: conv32_3,
+ conv64_down: conv64_down,
+ conv64_1: conv64_1,
+ conv64_2: conv64_2,
+ conv64_3: conv64_3,
+ conv128_down: conv128_down,
+ conv128_1: conv128_1,
+ conv128_2: conv128_2,
+ conv256_down: conv256_down,
+ conv256_1: conv256_1,
+ conv256_2: conv256_2,
+ conv256_down_out: conv256_down_out,
+ fc: fc
+ }];
+ }
+ });
+ });
+ }
+
+ function normalize(x) {
+ return tidy(function () {
+ var avg_r = fill([1, 150, 150, 1], 122.782);
+ var avg_g = fill([1, 150, 150, 1], 117.001);
+ var avg_b = fill([1, 150, 150, 1], 104.298);
+ var avg_rgb = concat([avg_r, avg_g, avg_b], 3);
+ return div(sub(x, avg_rgb), scalar(256));
+ });
+ }
+
+ function residual(x, params) {
+ var out = conv$1(x, params.conv1);
+ out = convNoRelu(out, params.conv2);
+ out = add(out, x);
+ out = relu(out);
+ return out;
+ }
+ function residualDown(x, params) {
+ var out = convDown(x, params.conv1);
+ out = convNoRelu(out, params.conv2);
+ var pooled = avgPool(x, 2, 2, 'valid');
+ var zeros$$1 = zeros(pooled.shape);
+ var isPad = pooled.shape[3] !== out.shape[3];
+ var isAdjustShape = pooled.shape[1] !== out.shape[1] || pooled.shape[2] !== out.shape[2];
+ if (isAdjustShape) {
+ var padShapeX = out.shape.slice();
+ padShapeX[1] = 1;
+ var zerosW = zeros(padShapeX);
+ out = concat([out, zerosW], 1);
+ var padShapeY = out.shape.slice();
+ padShapeY[2] = 1;
+ var zerosH = zeros(padShapeY);
+ out = concat([out, zerosH], 2);
}
- return tostring(key);
- }
-
- //
- // autoseed()
- // Returns an object for autoseeding, using window.crypto and Node crypto
- // module if available.
- //
- function autoseed() {
- try {
- var out;
- if (nodecrypto && (out = nodecrypto.randomBytes)) {
- // The use of 'out' to remember randomBytes makes tight minified code.
- out = out(width);
- } else {
- out = new Uint8Array(width);
- (global.crypto || global.msCrypto).getRandomValues(out);
- }
- return tostring(out);
- } catch (e) {
- var browser = global.navigator,
- plugins = browser && browser.plugins;
- return [+new Date, global, plugins, global.screen, tostring(pool)];
+ pooled = isPad ? concat([pooled, zeros$$1], 3) : pooled;
+ out = add(pooled, out);
+ out = relu(out);
+ return out;
+ }
+
+ var FaceRecognitionNet = /** @class */ (function () {
+ function FaceRecognitionNet() {
}
- }
-
- //
- // tostring()
- // Converts an array of charcodes to a string
- //
- function tostring(a) {
- return String.fromCharCode.apply(0, a);
- }
-
- //
- // When seedrandom.js is loaded, we immediately mix a few bits
- // from the built-in RNG into the entropy pool. Because we do
- // not want to interfere with deterministic PRNG state later,
- // seedrandom will not call math.random on its own again after
- // initialization.
- //
- mixkey(math.random(), pool);
-
- //
- // Nodejs and AMD support: export the implementation as a module using
- // either convention.
- //
- if (module.exports) {
- module.exports = seedrandom;
- // When in node.js, try using crypto package for autoseeding.
- try {
- nodecrypto = crypto;
- } catch (ex) {}
- } else if ((typeof undefined) == 'function' && undefined.amd) {
- undefined(function() { return seedrandom; });
- }
-
- // End anonymous scope, and pass initial values.
- })(
- [], // pool: entropy pool starts empty
- Math // math: package containing random, pow, and seedrandom
- );
- });
-
- // A library of seedable RNGs implemented in Javascript.
- //
- // Usage:
- //
- // var seedrandom = require('seedrandom');
- // var random = seedrandom(1); // or any seed.
- // var x = random(); // 0 <= x < 1. Every bit is random.
- // var x = random.quick(); // 0 <= x < 1. 32 bits of randomness.
-
- // alea, a 53-bit multiply-with-carry generator by Johannes Baagøe.
- // Period: ~2^116
- // Reported to pass all BigCrush tests.
-
-
- // xor128, a pure xor-shift generator by George Marsaglia.
- // Period: 2^128-1.
- // Reported to fail: MatrixRank and LinearComp.
-
-
- // xorwow, George Marsaglia's 160-bit xor-shift combined plus weyl.
- // Period: 2^192-2^32
- // Reported to fail: CollisionOver, SimpPoker, and LinearComp.
-
-
- // xorshift7, by François Panneton and Pierre L'ecuyer, takes
- // a different approach: it adds robustness by allowing more shifts
- // than Marsaglia's original three. It is a 7-shift generator
- // with 256 bits, that passes BigCrush with no systmatic failures.
- // Period 2^256-1.
- // No systematic BigCrush failures reported.
-
-
- // xor4096, by Richard Brent, is a 4096-bit xor-shift with a
- // very long period that also adds a Weyl generator. It also passes
- // BigCrush with no systematic failures. Its long period may
- // be useful if you have many generators and need to avoid
- // collisions.
- // Period: 2^4128-2^32.
- // No systematic BigCrush failures reported.
-
-
- // Tyche-i, by Samuel Neves and Filipe Araujo, is a bit-shifting random
- // number generator derived from ChaCha, a modern stream cipher.
- // https://eden.dei.uc.pt/~sneves/pubs/2011-snfa2.pdf
- // Period: ~2^127
- // No systematic BigCrush failures reported.
-
-
- // The original ARC4-based prng included in this library.
- // Period: ~2^1600
-
-
- seedrandom.alea = alea;
- seedrandom.xor128 = xor128;
- seedrandom.xorwow = xorwow;
- seedrandom.xorshift7 = xorshift7;
- seedrandom.xor4096 = xor4096;
- seedrandom.tychei = tychei;
-
- var C__Users_user_dev_faceApi_js_node_modules_seedrandom = seedrandom;
-
- var seedrandom$1 = /*#__PURE__*/Object.freeze({
- default: C__Users_user_dev_faceApi_js_node_modules_seedrandom,
- __moduleExports: C__Users_user_dev_faceApi_js_node_modules_seedrandom
- });
-
- var MPRandGauss = (function () {
- function MPRandGauss(mean, stdDeviation, dtype, truncated, seed) {
- this.mean = mean;
- this.stdDev = stdDeviation;
- this.dtype = dtype;
- this.nextVal = NaN;
- this.truncated = truncated;
- if (this.truncated) {
- this.upper = this.mean + this.stdDev * 2;
- this.lower = this.mean - this.stdDev * 2;
- }
- var seedValue = seed ? seed : Math.random();
- this.random = undefined(seedValue.toString());
- }
- MPRandGauss.prototype.nextValue = function () {
- if (!isNaN(this.nextVal)) {
- var value = this.nextVal;
- this.nextVal = NaN;
- return value;
- }
- var resultX, resultY;
- var isValid = false;
- while (!isValid) {
- var v1 = void 0, v2 = void 0, s = void 0;
- do {
- v1 = 2 * this.random() - 1;
- v2 = 2 * this.random() - 1;
- s = v1 * v1 + v2 * v2;
- } while (s >= 1 || s === 0);
- var mul = Math.sqrt(-2.0 * Math.log(s) / s);
- resultX = this.mean + this.stdDev * v1 * mul;
- resultY = this.mean + this.stdDev * v2 * mul;
- if (!this.truncated || this.isValidTruncated(resultX)) {
- isValid = true;
- }
- }
- if (!this.truncated || this.isValidTruncated(resultY)) {
- this.nextVal = this.convertValue(resultY);
- }
- return this.convertValue(resultX);
- };
- MPRandGauss.prototype.convertValue = function (value) {
- if (this.dtype == null || this.dtype === 'float32') {
- return value;
- }
- return Math.round(value);
- };
- MPRandGauss.prototype.isValidTruncated = function (value) {
- return value <= this.upper && value >= this.lower;
- };
- return MPRandGauss;
- }());
-
- var __decorate$1 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var ReductionOps = (function () {
- function ReductionOps() {
- }
- ReductionOps.logSumExp = function (x, axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- assertArgumentsAreTensors({ x: x }, 'logSumExp');
- var axes = parseAxisParam(axis, x.shape);
- var xMax = x.max(axes, true);
- var a = x.sub(xMax);
- var b = a.exp();
- var c = b.sum(axes);
- var d = c.log();
- var res = xMax.reshape(d.shape).add(d);
- if (keepDims) {
- var newShape = expandShapeToKeepDim(res.shape, axes);
- return res.reshape(newShape);
- }
- return res;
- };
- ReductionOps.sum = function (x, axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- assertArgumentsAreTensors({ x: x }, 'sum');
- if (x.dtype === 'bool') {
- x = x.toInt();
- }
- var axes = parseAxisParam(axis, x.shape);
- var customOp = customGrad(function (x) {
- var permutation = getAxesPermutation(axes, x.rank);
- var reductionAxes = axes;
- var permutedX = x;
- if (permutation != null) {
- permutedX = x.transpose(permutation);
- reductionAxes =
- getInnerMostAxes(reductionAxes.length, x.rank);
- }
- var value = ENV.engine.runKernel(function (backend) { return backend.sum(permutedX, reductionAxes); }, { permutedX: permutedX });
- if (keepDims) {
- var newShape = expandShapeToKeepDim(value.shape, axes);
- value = value.reshape(newShape);
- }
- var gradFunc = function (dy) {
- var expandedDyShape = x.shape.slice();
- axes.forEach(function (axis) {
- expandedDyShape[axis] = 1;
- });
- var expandedDy = dy.reshape(expandedDyShape);
- var derX = expandedDy.mul(ones(x.shape, 'float32'));
- return derX;
- };
- return { value: value, gradFunc: gradFunc };
- });
- return customOp(x);
- };
- ReductionOps.mean = function (x, axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- assertArgumentsAreTensors({ x: x }, 'mean');
- var axes = parseAxisParam(axis, x.shape);
- var shapes = computeOutAndReduceShapes(x.shape, axes);
- var reduceShape = shapes[1];
- var reduceSize = sizeFromShape(reduceShape);
- var customOp = customGrad(function (x) {
- var reduceSizeScalar = scalar(reduceSize);
- var xReduce = reduceSizeScalar.dtype === x.dtype ?
- x :
- x.cast(reduceSizeScalar.dtype);
- var res = xReduce.div(reduceSizeScalar);
- var value = res.sum(axis, keepDims);
- var gradFunc = function (dy) {
- var expandedDyShape = x.shape.slice();
- axes.forEach(function (axis) {
- expandedDyShape[axis] = 1;
- });
- var expandedDy = dy.reshape(expandedDyShape);
- var derX = expandedDy.mul(ones(x.shape, 'float32')).div(reduceSizeScalar);
- return derX;
- };
- return { value: value, gradFunc: gradFunc };
- });
- return customOp(x);
- };
- ReductionOps.min = function (x, axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- assertArgumentsAreTensors({ x: x }, 'min');
- var origAxes = parseAxisParam(axis, x.shape);
- var axes = origAxes;
- var permutedAxes = getAxesPermutation(axes, x.rank);
- if (permutedAxes != null) {
- x = x.transpose(permutedAxes);
- axes = getInnerMostAxes(axes.length, x.rank);
- }
- var res = ENV.engine.runKernel(function (backend) { return backend.min(x, axes); }, { x: x });
- if (keepDims) {
- var newShape = expandShapeToKeepDim(res.shape, origAxes);
- return res.reshape(newShape);
- }
- return res;
- };
- ReductionOps.max = function (x, axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- assertArgumentsAreTensors({ x: x }, 'max');
- var origAxes = parseAxisParam(axis, x.shape);
- var axes = origAxes;
- var permutedAxes = getAxesPermutation(axes, x.rank);
- if (permutedAxes != null) {
- x = x.transpose(permutedAxes);
- axes = getInnerMostAxes(axes.length, x.rank);
- }
- var res = ENV.engine.runKernel(function (backend) { return backend.max(x, axes); }, { x: x });
- if (keepDims) {
- var newShape = expandShapeToKeepDim(res.shape, origAxes);
- return res.reshape(newShape);
- }
- return res;
- };
- ReductionOps.argMin = function (x, axis) {
- if (axis === void 0) { axis = 0; }
- assertArgumentsAreTensors({ x: x }, 'argMin');
- if (axis == null) {
- axis = 0;
- }
- var axes = parseAxisParam(axis, x.shape);
- var permutedAxes = getAxesPermutation(axes, x.rank);
- if (permutedAxes != null) {
- x = x.transpose(permutedAxes);
- axes = getInnerMostAxes(axes.length, x.rank);
- }
- return ENV.engine.runKernel(function (backend) { return backend.argMin(x, axes[0]); }, { x: x });
- };
- ReductionOps.argMax = function (x, axis) {
- if (axis === void 0) { axis = 0; }
- assertArgumentsAreTensors({ x: x }, 'argMax');
- if (axis == null) {
- axis = 0;
- }
- var axes = parseAxisParam(axis, x.shape);
- var permutedAxes = getAxesPermutation(axes, x.rank);
- if (permutedAxes != null) {
- x = x.transpose(permutedAxes);
- axes = getInnerMostAxes(axes.length, x.rank);
- }
- return ENV.engine.runKernel(function (backend) { return backend.argMax(x, axes[0]); }, { x: x });
- };
- ReductionOps.moments = function (x, axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- assertArgumentsAreTensors({ x: x }, 'moments');
- var axes = parseAxisParam(axis, x.shape);
- var mean$$1 = x.mean(axes, keepDims);
- var keepDimsShape = mean$$1.shape;
- if (!keepDims) {
- keepDimsShape = expandShapeToKeepDim(mean$$1.shape, axes);
- }
- var devSquared = x.toFloat().sub(mean$$1.reshape(keepDimsShape)).square();
- var variance = devSquared.mean(axes, keepDims);
- return { mean: mean$$1, variance: variance };
- };
- ReductionOps.unsortedSegmentSum = function (x, segmentIds, numSegments, axis) {
- if (axis === void 0) { axis = 0; }
- assertArgumentsAreTensors({ x: x, segmentIds: segmentIds }, 'unsortedSegmentSum');
- assert(segmentIds.dtype === 'int32', 'Segment Ids must be of dtype `int32`');
- axis = parseAxisParam(axis, x.shape)[0];
- var res = [];
- var dim = segmentIds.shape[0];
- var newShape = [];
- for (var i = 0; i < x.shape.length; i++) {
- if (i === axis) {
- newShape.push(dim);
- }
- else {
- newShape.push(1);
- }
- }
- var reshapedSegmentIds = reshape(segmentIds, newShape);
- for (var i = 0; i < numSegments; i++) {
- var segmentId = scalar(i, 'int32');
- var mask = equal(segmentId, reshapedSegmentIds).asType('float32');
- var sum$$1 = mask.mul(x).sum(axis);
- res.push(sum$$1);
- }
- return stack(res, axis);
- };
- __decorate$1([
- doc({ heading: 'Operations', subheading: 'Reduction' }),
- operation
- ], ReductionOps, "logSumExp", null);
- __decorate$1([
- doc({ heading: 'Operations', subheading: 'Reduction' }),
- operation
- ], ReductionOps, "sum", null);
- __decorate$1([
- doc({ heading: 'Operations', subheading: 'Reduction' }),
- operation
- ], ReductionOps, "mean", null);
- __decorate$1([
- doc({ heading: 'Operations', subheading: 'Reduction' }),
- operation
- ], ReductionOps, "min", null);
- __decorate$1([
- doc({ heading: 'Operations', subheading: 'Reduction' }),
- operation
- ], ReductionOps, "max", null);
- __decorate$1([
- doc({ heading: 'Operations', subheading: 'Reduction' }),
- operation
- ], ReductionOps, "argMin", null);
- __decorate$1([
- doc({ heading: 'Operations', subheading: 'Reduction' }),
- operation
- ], ReductionOps, "argMax", null);
- __decorate$1([
- doc({ heading: 'Operations', subheading: 'Normalization' }),
- operation
- ], ReductionOps, "moments", null);
- __decorate$1([
- doc({ heading: 'Operations', subheading: 'Reduction' }),
- operation
- ], ReductionOps, "unsortedSegmentSum", null);
- return ReductionOps;
- }());
-
- var __decorate$2 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var ArrayOps = (function () {
- function ArrayOps() {
- }
- ArrayOps.tensor = function (values, shape, dtype) {
- if (dtype === void 0) { dtype = 'float32'; }
- var inferredShape = inferShape(values);
- if (shape != null && inferredShape.length !== 1) {
- assertShapesMatch(shape, inferredShape, "Error creating a new Tensor. " +
- ("Inferred shape (" + inferredShape + ") does not match the ") +
- ("provided shape (" + shape + "). "));
- }
- if (!isTypedArray(values) && !Array.isArray(values)) {
- values = [values];
- }
- shape = shape || inferredShape;
- return Tensor.make(shape, { values: toTypedArray(values, dtype) }, dtype);
- };
- ArrayOps.scalar = function (value, dtype) {
- if (dtype === void 0) { dtype = 'float32'; }
- if (isTypedArray(value) || Array.isArray(value)) {
- throw new Error('Error creating a new Scalar: value must be a primitive ' +
- '(number|boolean)');
- }
- return ArrayOps.tensor(value, [], dtype);
- };
- ArrayOps.tensor1d = function (values, dtype) {
- if (dtype === void 0) { dtype = 'float32'; }
- var inferredShape = inferShape(values);
- if (inferredShape.length !== 1) {
- throw new Error('tensor1d() requires values to be a flat/TypedArray');
- }
- return ArrayOps.tensor(values, inferredShape, dtype);
- };
- ArrayOps.tensor2d = function (values, shape, dtype) {
- if (dtype === void 0) { dtype = 'float32'; }
- if (shape != null && shape.length !== 2) {
- throw new Error('tensor2d() requires shape to have two numbers');
- }
- var inferredShape = inferShape(values);
- if (inferredShape.length !== 2 && inferredShape.length !== 1) {
- throw new Error('tensor2d() requires values to be number[][] or flat/TypedArray');
- }
- if (inferredShape.length === 1 && shape == null) {
- throw new Error('tensor2d() requires shape to be provided when `values` ' +
- 'are a flat/TypedArray');
- }
- shape = shape || inferredShape;
- return ArrayOps.tensor(values, shape, dtype);
- };
- ArrayOps.tensor3d = function (values, shape, dtype) {
- if (dtype === void 0) { dtype = 'float32'; }
- if (shape != null && shape.length !== 3) {
- throw new Error('tensor3d() requires shape to have three numbers');
- }
- var inferredShape = inferShape(values);
- if (inferredShape.length !== 3 && inferredShape.length !== 1) {
- throw new Error('tensor3d() requires values to be number[][][] or flat/TypedArray');
- }
- if (inferredShape.length === 1 && shape == null) {
- throw new Error('tensor3d() requires shape to be provided when `values` ' +
- 'are a flat array');
- }
- shape = shape || inferredShape;
- return ArrayOps.tensor(values, shape, dtype);
- };
- ArrayOps.tensor4d = function (values, shape, dtype) {
- if (dtype === void 0) { dtype = 'float32'; }
- if (shape != null && shape.length !== 4) {
- throw new Error('tensor4d() requires shape to have four numbers');
- }
- var inferredShape = inferShape(values);
- if (inferredShape.length !== 4 && inferredShape.length !== 1) {
- throw new Error('tensor4d() requires values to be number[][][][] or flat/TypedArray');
- }
- if (inferredShape.length === 1 && shape == null) {
- throw new Error('tensor4d() requires shape to be provided when `values` ' +
- 'are a flat array');
- }
- shape = shape || inferredShape;
- return ArrayOps.tensor(values, shape, dtype);
- };
- ArrayOps.ones = function (shape, dtype) {
- if (dtype === void 0) { dtype = 'float32'; }
- var values = makeOnesTypedArray(sizeFromShape(shape), dtype);
- return Tensor.make(shape, { values: values }, dtype);
- };
- ArrayOps.zeros = function (shape, dtype) {
- if (dtype === void 0) { dtype = 'float32'; }
- var values = makeZerosTypedArray(sizeFromShape(shape), dtype);
- return Tensor.make(shape, { values: values }, dtype);
- };
- ArrayOps.fill = function (shape, value, dtype) {
- if (dtype === void 0) { dtype = 'float32'; }
- var values = getTypedArrayFromDType(dtype, sizeFromShape(shape));
- values.fill(value);
- return Tensor.make(shape, { values: values }, dtype);
- };
- ArrayOps.onesLike = function (x) {
- assertArgumentsAreTensors({ x: x }, 'onesLike');
- return ArrayOps.ones(x.shape, x.dtype);
- };
- ArrayOps.zerosLike = function (x) {
- assertArgumentsAreTensors({ x: x }, 'zerosLike');
- return ArrayOps.zeros(x.shape, x.dtype);
- };
- ArrayOps.clone = function (x) {
- assertArgumentsAreTensors({ x: x }, 'clone');
- var der = function (dy) {
- return { x: function () { return dy.toFloat(); } };
- };
- return ENV.engine.runKernel(function (backend) {
- return Tensor.make(x.shape, { dataId: x.dataId }, x.dtype);
- }, { x: x }, der);
- };
- ArrayOps.eye = function (numRows, numColumns, batchShape, dtype) {
- if (dtype === void 0) { dtype = 'float32'; }
- if (numColumns == null) {
- numColumns = numRows;
- }
- var buffer = ArrayOps.buffer([numRows, numColumns], dtype);
- var n = numRows <= numColumns ? numRows : numColumns;
- for (var i = 0; i < n; ++i) {
- buffer.set(1, i, i);
- }
- var out = buffer.toTensor().as2D(numRows, numColumns);
- if (batchShape == null) {
- return out;
- }
- else {
- if (batchShape.length === 1) {
- return ArrayOps.tile(ArrayOps.expandDims(out, 0), [batchShape[0], 1, 1]);
- }
- else if (batchShape.length === 2) {
- return ArrayOps.tile(ArrayOps.expandDims(ArrayOps.expandDims(out, 0), 0), [batchShape[0], batchShape[1], 1, 1]);
- }
- else {
- throw new Error("eye() currently supports only 1D and 2D " +
- ("batchShapes, but received " + batchShape.length + "D."));
- }
- }
- };
- ArrayOps.randomNormal = function (shape, mean, stdDev, dtype, seed) {
- if (mean === void 0) { mean = 0; }
- if (stdDev === void 0) { stdDev = 1; }
- if (dtype != null && dtype === 'bool') {
- throw new Error("Unsupported data type " + dtype);
- }
- var randGauss = new MPRandGauss(mean, stdDev, dtype, false, seed);
- var res = ArrayOps.buffer(shape, dtype);
- for (var i = 0; i < res.values.length; i++) {
- res.values[i] = randGauss.nextValue();
- }
- return res.toTensor();
- };
- ArrayOps.truncatedNormal = function (shape, mean, stdDev, dtype, seed) {
- if (mean === void 0) { mean = 0; }
- if (stdDev === void 0) { stdDev = 1; }
- if (dtype != null && dtype === 'bool') {
- throw new Error("Unsupported data type " + dtype);
- }
- var randGauss = new MPRandGauss(mean, stdDev, dtype, true, seed);
- var res = ArrayOps.buffer(shape, dtype);
- for (var i = 0; i < res.values.length; i++) {
- res.values[i] = randGauss.nextValue();
- }
- return res.toTensor();
- };
- ArrayOps.randomUniform = function (shape, minval, maxval, dtype) {
- if (minval === void 0) { minval = 0; }
- if (maxval === void 0) { maxval = 1; }
- if (dtype === void 0) { dtype = 'float32'; }
- var res = ArrayOps.buffer(shape, dtype);
- for (var i = 0; i < res.values.length; i++) {
- res.values[i] = randUniform(minval, maxval);
- }
- return res.toTensor();
- };
- ArrayOps.rand = function (shape, randFunction, dtype) {
- var size = sizeFromShape(shape);
- var values = null;
- if (dtype == null || dtype === 'float32') {
- values = new Float32Array(size);
- }
- else if (dtype === 'int32') {
- values = new Int32Array(size);
- }
- else if (dtype === 'bool') {
- values = new Uint8Array(size);
- }
- else {
- throw new Error("Unknown data type " + dtype);
- }
- for (var i = 0; i < size; i++) {
- values[i] = randFunction();
- }
- return Tensor.make(shape, { values: values }, dtype);
- };
- ArrayOps.multinomial = function (logits, numSamples, seed, normalized) {
- if (normalized === void 0) { normalized = false; }
- assertArgumentsAreTensors({ logits: logits }, 'multinomial');
- var numOutcomes = logits.size;
- var origRank = logits.rank;
- if (numOutcomes < 2) {
- throw new Error("Error in multinomial: you need at least 2 outcomes, but got " +
- (numOutcomes + "."));
- }
- if (origRank > 2) {
- throw new Error("Rank of probabilities must be 1 or 2, but is " + origRank);
- }
- seed = seed || Math.random();
- var logits2D = origRank === 1 ? logits.as2D(1, -1) : logits;
- var res = ENV.engine.runKernel(function (backend) { return backend.multinomial(logits2D, normalized, numSamples, seed); }, { logits2D: logits2D });
- return origRank === 1 ? res.as1D() : res;
- };
- ArrayOps.oneHot = function (indices, depth, onValue, offValue) {
- if (onValue === void 0) { onValue = 1; }
- if (offValue === void 0) { offValue = 0; }
- assert(indices.dtype === 'int32', 'Indices must be of dtype `int32`');
- if (depth < 2) {
- throw new Error("Error in oneHot: depth must be >=2, but it is " + depth);
- }
- return ENV.engine.runKernel(function (backend) { return backend.oneHot(indices, depth, onValue, offValue); }, { indices: indices });
- };
- ArrayOps.fromPixels = function (pixels, numChannels) {
- if (numChannels === void 0) { numChannels = 3; }
- if (numChannels > 4) {
- throw new Error('Cannot construct Tensor with more than 4 channels from pixels.');
- }
- return ENV.engine.fromPixels(pixels, numChannels);
- };
- ArrayOps.toPixels = function (img, canvas) {
- return __awaiter(this, void 0, void 0, function () {
- var _a, height, width, depth, minTensor, maxTensor, min, max, data, multiplier, bytes, i, r, g, b, a, j, ctx, imageData;
- return __generator(this, function (_b) {
- switch (_b.label) {
- case 0:
- assertArgumentsAreTensors({ img: img }, 'toPixels');
- if (img.rank !== 2 && img.rank !== 3) {
- throw new Error("toPixels only supports rank 2 or 3 tensors, got rank " + img.rank + ".");
- }
- _a = img.shape.slice(0, 2), height = _a[0], width = _a[1];
- depth = img.rank === 2 ? 1 : img.shape[2];
- if (depth > 4 || depth === 2) {
- throw new Error("toPixels only supports depth of size " +
- ("1, 3 or 4 but got " + depth));
- }
- minTensor = img.min();
- maxTensor = img.max();
- return [4, minTensor.data()];
- case 1:
- min = (_b.sent())[0];
- return [4, maxTensor.data()];
- case 2:
- max = (_b.sent())[0];
- minTensor.dispose();
- maxTensor.dispose();
- if (img.dtype === 'float32') {
- if (min < 0 || max > 1) {
- throw new Error("Tensor values for a float32 Tensor must be in the " +
- ("range [0 - 1] but got range [" + min + " - " + max + "]."));
- }
- }
- else if (img.dtype === 'int32') {
- if (min < 0 || max > 255) {
- throw new Error("Tensor values for a int32 Tensor must be in the " +
- ("range [0 - 255] but got range [" + min + " - " + max + "]."));
- }
- }
- else {
- throw new Error("Unsupported type for toPixels: " + img.dtype + "." +
- " Please use float32 or int32 tensors.");
- }
- return [4, img.data()];
- case 3:
- data = _b.sent();
- multiplier = img.dtype === 'float32' ? 255 : 1;
- bytes = new Uint8ClampedArray(width * height * 4);
- for (i = 0; i < height * width; ++i) {
- r = void 0, g = void 0, b = void 0, a = void 0;
- if (depth === 1) {
- r = data[i] * multiplier;
- g = data[i] * multiplier;
- b = data[i] * multiplier;
- a = 255;
- }
- else if (depth === 3) {
- r = data[i * 3] * multiplier;
- g = data[i * 3 + 1] * multiplier;
- b = data[i * 3 + 2] * multiplier;
- a = 255;
- }
- else if (depth === 4) {
- r = data[i * 4] * multiplier;
- g = data[i * 4 + 1] * multiplier;
- b = data[i * 4 + 2] * multiplier;
- a = data[i * 4 + 3] * multiplier;
- }
- j = i * 4;
- bytes[j + 0] = Math.round(r);
- bytes[j + 1] = Math.round(g);
- bytes[j + 2] = Math.round(b);
- bytes[j + 3] = Math.round(a);
- }
- if (canvas != null) {
- canvas.width = width;
- canvas.height = height;
- ctx = canvas.getContext('2d');
- imageData = new ImageData(bytes, width, height);
- ctx.putImageData(imageData, 0, 0);
- }
- return [2, bytes];
- }
- });
- });
- };
- ArrayOps.reshape = function (x, shape) {
- assertArgumentsAreTensors({ x: x }, 'reshape');
- shape = inferFromImplicitShape(shape, x.size);
- assert(x.size === sizeFromShape(shape), 'new shape and old shape must have the same number of elements.');
- var grad = function (dy) {
- return { x: function () { return dy.reshape(x.shape); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.reshape(x, shape); }, { x: x }, grad);
- };
- ArrayOps.squeeze = function (x, axis) {
- assertArgumentsAreTensors({ x: x }, 'squeeze');
- return ArrayOps.reshape(x, squeezeShape(x.shape, axis).newShape);
- };
- ArrayOps.cast = function (x, dtype) {
- assertArgumentsAreTensors({ x: x }, 'cast');
- var grad = function (dy) {
- return { x: function () { return dy.clone(); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.cast(x, dtype); }, { x: x }, grad);
- };
- ArrayOps.tile = function (x, reps) {
- assertArgumentsAreTensors({ x: x }, 'tile');
- assert(x.rank === reps.length, "Error in transpose: rank of input " + x.rank + " " +
- ("must match length of reps " + reps + "."));
- var grad = function (dy) {
- var derX = function () {
- var xGrad = ArrayOps.zerosLike(x);
- if (x.rank === 1) {
- for (var i = 0; i < reps[0]; ++i) {
- xGrad = xGrad.add(dy.slice([i * x.shape[0]], [x.shape[0]]));
- }
- }
- else if (x.rank === 2) {
- for (var i = 0; i < reps[0]; ++i) {
- for (var j = 0; j < reps[1]; ++j) {
- xGrad = xGrad.add(dy.slice([i * x.shape[0], j * x.shape[1]], [x.shape[0], x.shape[1]]));
- }
- }
- }
- else if (x.rank === 3) {
- for (var i = 0; i < reps[0]; ++i) {
- for (var j = 0; j < reps[1]; ++j) {
- for (var k = 0; k < reps[2]; ++k) {
- xGrad = xGrad.add(dy.slice([i * x.shape[0], j * x.shape[1], k * x.shape[2]], [x.shape[0], x.shape[1], x.shape[2]]));
- }
- }
- }
- }
- else if (x.rank === 4) {
- for (var i = 0; i < reps[0]; ++i) {
- for (var j = 0; j < reps[1]; ++j) {
- for (var k = 0; k < reps[2]; ++k) {
- for (var l = 0; l < reps[3]; ++l) {
- xGrad = xGrad.add(dy.slice([
- i * x.shape[0], j * x.shape[1], k * x.shape[2],
- l * x.shape[3]
- ], [x.shape[0], x.shape[1], x.shape[2], x.shape[3]]));
- }
- }
- }
- }
- }
- else {
- throw new Error("Gradient for tile operation is not implemented for rank-" +
- (x.rank + " tensors yet."));
- }
- return xGrad;
- };
- return { x: derX };
- };
- return ENV.engine.runKernel(function (backend) { return backend.tile(x, reps); }, { x: x }, grad);
- };
- ArrayOps.gather = function (x, indices, axis) {
- if (axis === void 0) { axis = 0; }
- assertArgumentsAreTensors({ x: x, indices: indices }, 'gather');
- assert(indices.dtype === 'int32', 'Indices must be of dtype `int32`');
- axis = parseAxisParam(axis, x.shape)[0];
- var grad = function (dy) {
- var derX = function () {
- return ReductionOps.unsortedSegmentSum(dy, indices, x.shape[axis], axis);
- };
- return { x: derX };
- };
- return ENV.engine.runKernel(function (backend) { return backend.gather(x, indices, axis); }, { x: x }, grad);
- };
- ArrayOps.pad1d = function (x, paddings, constantValue) {
- if (constantValue === void 0) { constantValue = 0; }
- assert(paddings.length === 2, 'Invalid number of paddings. Must be length of 2.');
- return ArrayOps.pad(x, [paddings], constantValue);
- };
- ArrayOps.pad2d = function (x, paddings, constantValue) {
- if (constantValue === void 0) { constantValue = 0; }
- assert(paddings.length === 2 && paddings[0].length === 2 &&
- paddings[1].length === 2, 'Invalid number of paddings. Must be length of 2 each.');
- return ArrayOps.pad(x, paddings, constantValue);
- };
- ArrayOps.pad3d = function (x, paddings, constantValue) {
- if (constantValue === void 0) { constantValue = 0; }
- assert(paddings.length === 3 && paddings[0].length === 2 &&
- paddings[1].length === 2 && paddings[2].length === 2, 'Invalid number of paddings. Must be length of 2 each.');
- return ArrayOps.pad(x, paddings, constantValue);
- };
- ArrayOps.pad4d = function (x, paddings, constantValue) {
- if (constantValue === void 0) { constantValue = 0; }
- assert(paddings.length === 4 && paddings[0].length === 2 &&
- paddings[1].length === 2 && paddings[2].length === 2 &&
- paddings[3].length === 2, 'Invalid number of paddings. Must be length of 2 each.');
- return ArrayOps.pad(x, paddings, constantValue);
- };
- ArrayOps.pad = function (x, paddings, constantValue) {
- if (constantValue === void 0) { constantValue = 0; }
- assertArgumentsAreTensors({ x: x }, 'pad');
- if (x.rank === 0) {
- throw new Error('pad(scalar) is not defined. Pass non-scalar to pad');
- }
- var begin = paddings.map(function (p) { return p[0]; });
- var grad = function (dy) {
- return { x: function () { return dy.slice(begin, x.shape); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.pad(x, paddings, constantValue); }, { x: x }, grad);
- };
- ArrayOps.stack = function (tensors, axis) {
- if (axis === void 0) { axis = 0; }
- assertArgumentsAreTensors({ tensors: tensors }, 'stack');
- assert(tensors.length >= 1, 'Pass at least one tensor to tf.stack');
- if (tensors.length === 1) {
- return tensors[0].expandDims(axis);
- }
- var rank = tensors[0].rank;
- var shape = tensors[0].shape;
- var dtype = tensors[0].dtype;
- assert(axis <= rank, 'Axis must be <= rank of the tensor');
- tensors.forEach(function (t) {
- assertShapesMatch(shape, t.shape, 'All tensors passed to stack must have matching shapes');
- });
- tensors.forEach(function (t) {
- assert(dtype === t.dtype, 'All tensors passed to stack must have matching dtypes');
- });
- var expandedTensors = tensors.map(function (t) { return t.expandDims(axis); });
- return ConcatOps.concat(expandedTensors, axis);
- };
- ArrayOps.unstack = function (value, axis) {
- if (axis === void 0) { axis = 0; }
- var num = value.shape[axis];
- var outputShape = Array(value.rank - 1).fill(0);
- var outIndex = 0;
- for (var i = 0; i < value.rank; i++) {
- if (i !== axis) {
- outputShape[outIndex] = value.shape[i];
- outIndex++;
- }
- }
- var splitSizes;
- splitSizes = Array(num).fill(1);
- var begin = Array(value.rank).fill(0);
- var size = value.shape.slice();
- return splitSizes.map(function (s) {
- size[axis] = s;
- var slice = value.slice(begin, size);
- begin[axis] += s;
- return slice.reshape(outputShape);
- });
- };
- ArrayOps.split = function (x, numOrSizeSplits, axis) {
- if (axis === void 0) { axis = 0; }
- assertArgumentsAreTensors({ x: x }, 'split');
- axis = parseAxisParam(axis, x.shape)[0];
- var splitSizes;
- if (typeof (numOrSizeSplits) === 'number') {
- assert(x.shape[axis] % numOrSizeSplits === 0, 'Number of splits must evenly divide the axis.');
- splitSizes = Array(numOrSizeSplits).fill(x.shape[axis] / numOrSizeSplits);
- }
- else {
- assert(x.shape[axis] === numOrSizeSplits.reduce(function (a, b) { return a + b; }), 'The sum of sizes must match the size of the axis dimension.');
- splitSizes = numOrSizeSplits;
- }
- var begin = Array(x.rank).fill(0);
- var size = x.shape.slice();
- return splitSizes.map(function (s) {
- size[axis] = s;
- var slice = x.slice(begin, size);
- begin[axis] += s;
- return slice;
- });
- };
- ArrayOps.cumsum = function (x, axis, exclusive, reverse) {
- if (axis === void 0) { axis = 0; }
- if (exclusive === void 0) { exclusive = false; }
- if (reverse === void 0) { reverse = false; }
- assertArgumentsAreTensors({ x: x }, 'cumsum');
- var permutation = getAxesPermutation([axis], x.rank);
- var permutedX = x;
- if (permutation != null) {
- permutedX = x.transpose(permutation);
- }
- var grad = function (dy) {
- return { permutedX: function () { return dy.cumsum(axis, exclusive, !reverse); } };
- };
- var value = ENV.engine.runKernel(function (backend) { return backend.cumsum(permutedX, axis, exclusive, reverse); }, { permutedX: permutedX }, grad);
- if (permutation != null) {
- value = value.transpose(permutation);
- }
- return value;
- };
- ArrayOps.expandDims = function (x, axis) {
- if (axis === void 0) { axis = 0; }
- assertArgumentsAreTensors({ x: x }, 'expandDims');
- assert(axis <= x.rank, 'Axis must be <= rank of the tensor');
- var newShape = x.shape.slice();
- newShape.splice(axis, 0, 1);
- return ArrayOps.reshape(x, newShape);
- };
- ArrayOps.linspace = function (start, stop, num) {
- if (num === 0) {
- throw new Error('Cannot request zero samples');
- }
- var step = (stop - start) / (num - 1);
- var values = makeZerosTypedArray(num, 'float32');
- values[0] = start;
- for (var i = 1; i < values.length; i++) {
- values[i] = values[i - 1] + step;
- }
- return ArrayOps.tensor1d(values, 'float32');
- };
- ArrayOps.range = function (start, stop, step, dtype) {
- if (step === void 0) { step = 1; }
- if (dtype === void 0) { dtype = 'float32'; }
- if (step === 0) {
- throw new Error('Cannot have a step of zero');
- }
- var sameStartStop = start === stop;
- var increasingRangeNegativeStep = start < stop && step < 0;
- var decreasingRangePositiveStep = stop < start && step > 1;
- if (sameStartStop || increasingRangeNegativeStep ||
- decreasingRangePositiveStep) {
- return ArrayOps.zeros([0], dtype);
- }
- var numElements = Math.abs(Math.ceil((stop - start) / step));
- var values = makeZerosTypedArray(numElements, dtype);
- if (stop < start && step === 1) {
- step = -1;
- }
- values[0] = start;
- for (var i = 1; i < values.length; i++) {
- values[i] = values[i - 1] + step;
- }
- return ArrayOps.tensor1d(values, dtype);
- };
- ArrayOps.buffer = function (shape, dtype, values) {
- if (dtype === void 0) { dtype = 'float32'; }
- return new TensorBuffer(shape, dtype, values);
- };
- ArrayOps.print = function (x, verbose) {
- if (verbose === void 0) { verbose = false; }
- console.log(tensorToString(x, verbose));
- };
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], ArrayOps, "tensor", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], ArrayOps, "scalar", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], ArrayOps, "tensor1d", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], ArrayOps, "tensor2d", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], ArrayOps, "tensor3d", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], ArrayOps, "tensor4d", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "ones", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "zeros", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "fill", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "onesLike", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "zerosLike", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "clone", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "eye", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "randomNormal", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "truncatedNormal", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "randomUniform", null);
- __decorate$2([
- operation
- ], ArrayOps, "rand", null);
- __decorate$2([
- operation
- ], ArrayOps, "multinomial", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "oneHot", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' }),
- operation
- ], ArrayOps, "fromPixels", null);
- __decorate$2([
- doc({ heading: 'Visualization' })
- ], ArrayOps, "toPixels", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Transformations' }),
- operation
- ], ArrayOps, "reshape", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Transformations' })
- ], ArrayOps, "squeeze", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Transformations' }),
- operation
- ], ArrayOps, "cast", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Slicing and Joining' }),
- operation
- ], ArrayOps, "tile", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Slicing and Joining' }),
- operation
- ], ArrayOps, "gather", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Transformations' }),
- operation
- ], ArrayOps, "pad", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Slicing and Joining' }),
- operation
- ], ArrayOps, "stack", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Slicing and Joining' }),
- operation
- ], ArrayOps, "unstack", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Slicing and Joining' }),
- operation
- ], ArrayOps, "split", null);
- __decorate$2([
- doc({ heading: 'Operations', subheading: 'Scan' })
- ], ArrayOps, "cumsum", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Transformations' }),
- operation
- ], ArrayOps, "expandDims", null);
- __decorate$2([
- operation,
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], ArrayOps, "linspace", null);
- __decorate$2([
- operation,
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], ArrayOps, "range", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], ArrayOps, "buffer", null);
- __decorate$2([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], ArrayOps, "print", null);
- return ArrayOps;
- }());
- function makeZerosTypedArray(size, dtype) {
- if (dtype == null || dtype === 'float32') {
- return new Float32Array(size);
- }
- else if (dtype === 'int32') {
- return new Int32Array(size);
- }
- else if (dtype === 'bool') {
- return new Uint8Array(size);
- }
- else {
- throw new Error("Unknown data type $ {dtype}");
- }
- }
- function makeOnesTypedArray(size, dtype) {
- var array = makeZerosTypedArray(size, dtype);
- for (var i = 0; i < array.length; i++) {
- array[i] = 1;
- }
- return array;
- }
- function toTypedArray(a, dtype) {
- if (noConversionNeeded(a, dtype)) {
- return a;
- }
- if (Array.isArray(a)) {
- a = flatten(a);
- }
- return copyTypedArray(a, dtype);
- }
- function noConversionNeeded(a, dtype) {
- return (a instanceof Float32Array && dtype === 'float32') ||
- (a instanceof Int32Array && dtype === 'int32') ||
- (a instanceof Uint8Array && dtype === 'bool');
- }
-
- function getBroadcastDims(inShape, outShape) {
- var inRank = inShape.length;
- var dims = [];
- for (var i = 0; i < inRank; i++) {
- var dim = inRank - 1 - i;
- var a = inShape[dim] || 1;
- var b = outShape[outShape.length - 1 - i] || 1;
- if (b > 1 && a === 1) {
- dims.unshift(dim);
- }
- }
- return dims;
- }
- function getReductionAxes(inShape, outShape) {
- var result = [];
- for (var i = 0; i < outShape.length; i++) {
- var inDim = inShape[inShape.length - i - 1];
- var outAxis = outShape.length - i - 1;
- var outDim = outShape[outAxis];
- if (inDim == null || (inDim === 1 && outDim > 1)) {
- result.unshift(outAxis);
- }
- }
- return result;
- }
- function broadcastDimsAreOuter(dims) {
- for (var i = 0; i < dims.length; i++) {
- if (dims[i] !== i) {
- return false;
- }
- }
- return true;
- }
- function assertAndGetBroadcastShape(shapeA, shapeB) {
- var result = [];
- var errMsg = "Operands could not be broadcast together with shapes " +
- (shapeA + " and " + shapeB + ".");
- var l = Math.max(shapeA.length, shapeB.length);
- for (var i = 0; i < l; i++) {
- var a = shapeA[shapeA.length - i - 1] || 1;
- var b = shapeB[shapeB.length - i - 1] || 1;
- if (a > 1 && b > 1 && a !== b) {
- throw Error(errMsg);
- }
- result.unshift(Math.max(a, b));
- }
- return result;
- }
-
- var __decorate$3 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var BatchNormOps = (function () {
- function BatchNormOps() {
- }
- BatchNormOps.batchNormalization2d = function (x, mean$$1, variance, varianceEpsilon, scale, offset) {
- if (varianceEpsilon === void 0) { varianceEpsilon = .001; }
- assert(x.rank === 2, "Error in batchNormalization3D: x must be rank 3 but got rank " +
- (x.rank + "."));
- assert(mean$$1.rank === 2 || mean$$1.rank === 1, "Error in batchNormalization2D: mean must be rank 2 or rank 1 but " +
- ("got rank " + mean$$1.rank + "."));
- assert(variance.rank === 2 || variance.rank === 1, "Error in batchNormalization2D: variance must be rank 2 or rank 1 " +
- ("but got rank " + variance.rank + "."));
- if (scale != null) {
- assert(scale.rank === 2 || scale.rank === 1, "Error in batchNormalization2D: scale must be rank 2 or rank 1 " +
- ("but got rank " + scale.rank + "."));
- }
- if (offset != null) {
- assert(offset.rank === 2 || offset.rank === 1, "Error in batchNormalization2D: offset must be rank 2 or rank 1 " +
- ("but got rank " + offset.rank + "."));
- }
- return BatchNormOps.batchNormalization(x, mean$$1, variance, varianceEpsilon, scale, offset);
- };
- BatchNormOps.batchNormalization3d = function (x, mean$$1, variance, varianceEpsilon, scale, offset) {
- if (varianceEpsilon === void 0) { varianceEpsilon = .001; }
- assert(x.rank === 3, "Error in batchNormalization3D: x must be rank 3 but got rank " +
- (x.rank + "."));
- assert(mean$$1.rank === 3 || mean$$1.rank === 1, "Error in batchNormalization3D: mean must be rank 3 or rank 1 but " +
- ("got rank " + mean$$1.rank + "."));
- assert(variance.rank === 3 || variance.rank === 1, "Error in batchNormalization3D: variance must be rank 3 or rank 1 " +
- ("but got rank " + variance.rank + "."));
- if (scale != null) {
- assert(scale.rank === 3 || scale.rank === 1, "Error in batchNormalization3D: scale must be rank 3 or rank 1 " +
- ("but got rank " + scale.rank + "."));
- }
- if (offset != null) {
- assert(offset.rank === 3 || offset.rank === 1, "Error in batchNormalization3D: offset must be rank 3 or rank 1 " +
- ("but got rank " + offset.rank + "."));
- }
- return BatchNormOps.batchNormalization(x, mean$$1, variance, varianceEpsilon, scale, offset);
- };
- BatchNormOps.batchNormalization4d = function (x, mean$$1, variance, varianceEpsilon, scale, offset) {
- if (varianceEpsilon === void 0) { varianceEpsilon = .001; }
- assert(x.rank === 4, "Error in batchNormalization4D: x must be rank 4 but got rank " +
- (x.rank + "."));
- assert(mean$$1.rank === 4 || mean$$1.rank === 1, "Error in batchNormalization4D: mean must be rank 4 or rank 1 but " +
- ("got rank " + mean$$1.rank + "."));
- assert(variance.rank === 4 || variance.rank === 1, "Error in batchNormalization4D: variance must be rank 4 or rank 1 " +
- ("but got rank " + variance.rank + "."));
- if (scale != null) {
- assert(scale.rank === 4 || scale.rank === 1, "Error in batchNormalization4D: scale must be rank 4 or rank 1 " +
- ("but got rank " + scale.rank + "."));
- }
- if (offset != null) {
- assert(offset.rank === 4 || offset.rank === 1, "Error in batchNormalization4D: offset must be rank 4 or rank 1 " +
- ("but got rank " + offset.rank + "."));
- }
- return BatchNormOps.batchNormalization(x, mean$$1, variance, varianceEpsilon, scale, offset);
- };
- BatchNormOps.batchNormalization = function (x, mean$$1, variance, varianceEpsilon, scale, offset) {
- if (varianceEpsilon === void 0) { varianceEpsilon = .001; }
- assertArgumentsAreTensors({ x: x, mean: mean$$1, variance: variance }, 'batchNormalization');
- if (scale != null) {
- assertArgumentsAreTensors({ scale: scale }, 'batchNormalization');
- }
- if (offset != null) {
- assertArgumentsAreTensors({ offset: offset }, 'batchNormalization');
- }
- assert(mean$$1.rank === variance.rank, 'Batch normalization gradient requires mean and variance to have ' +
- 'equal ranks.');
- assert(offset == null || mean$$1.rank === offset.rank, 'Batch normalization gradient requires mean and offset to have ' +
- 'equal ranks.');
- assert(scale == null || mean$$1.rank === scale.rank, 'Batch normalization gradient requires mean and scale to have ' +
- 'equal ranks.');
- var x4D;
- if (x.rank === 0 || x.rank === 1) {
- x4D = x.as4D(1, 1, 1, x.size);
- }
- else if (x.rank === 2) {
- x4D = x.as4D(1, 1, x.shape[0], x.shape[1]);
- }
- else if (x.rank === 3) {
- x4D = x.as4D(1, x.shape[0], x.shape[1], x.shape[2]);
- }
- else {
- x4D = x;
- }
- var der = function (dy) {
- var scaleValue = scale == null ? ArrayOps.scalar(1) : scale;
- var reductionAxes = getReductionAxes(mean$$1.shape, x4D.shape);
- var tileShape = [];
- if (mean$$1.rank === 1) {
- for (var i = 0; i < x4D.shape.length - 1; ++i) {
- tileShape.push(x4D.shape[i]);
- }
- tileShape.push(1);
- }
- var xMinusMean = x.sub(mean$$1);
- var dyTimesScaleValue = dy.mul(scaleValue);
- var oneOverSqrtVariance = rsqrt(variance.add(ArrayOps.scalar(varianceEpsilon)));
- var minusHalfRCube = oneOverSqrtVariance.mul(oneOverSqrtVariance)
- .mul(oneOverSqrtVariance)
- .mul(ArrayOps.scalar(-0.5));
- var derX = function () {
- if (mean$$1.rank === 1) {
- return dy
- .mul(ArrayOps.tile(oneOverSqrtVariance.as4D(1, 1, 1, mean$$1.shape[0]), tileShape))
- .mul(scaleValue)
- .reshape(x.shape);
- }
- else {
- return dy.mul(oneOverSqrtVariance).mul(scaleValue).reshape(x.shape);
- }
- };
- var derMean = function () {
- var meanDer = oneOverSqrtVariance.mul(ArrayOps.scalar(-1)).mul(dyTimesScaleValue);
- if (mean$$1.rank === 1) {
- meanDer = meanDer.sum(reductionAxes);
- }
- return meanDer.reshape(mean$$1.shape);
- };
- var derVariance = function () {
- var varianceDer = minusHalfRCube.mul(xMinusMean).mul(dyTimesScaleValue);
- if (mean$$1.rank === 1) {
- varianceDer = varianceDer.sum(reductionAxes);
- }
- return varianceDer.reshape(mean$$1.shape);
- };
- var derScale = function () {
- var xMinusMean2TimesRsqrt = xMinusMean.mul(oneOverSqrtVariance);
- var scaleDer = dy.mul(xMinusMean2TimesRsqrt);
- if (mean$$1.rank === 1) {
- scaleDer = scaleDer.sum(reductionAxes);
- }
- return scaleDer.reshape(mean$$1.shape);
- };
- var derOffset = function () {
- var offsetDer = dy;
- if (mean$$1.rank === 1) {
- offsetDer = offsetDer.sum(reductionAxes);
- }
- return offsetDer.reshape(mean$$1.shape);
- };
- return {
- x: derX,
- mean: derMean,
- variance: derVariance,
- scale: derScale,
- offset: derOffset
- };
- };
- var res = ENV.engine.runKernel(function (backend) { return backend.batchNormalization(x4D, batchnormReshape4D(mean$$1), batchnormReshape4D(variance), varianceEpsilon, batchnormReshape4D(scale), batchnormReshape4D(offset)); }, { x: x, mean: mean$$1, variance: variance, scale: scale, offset: offset }, der);
- return res.reshape(x.shape);
- };
- __decorate$3([
- operation
- ], BatchNormOps, "batchNormalization2d", null);
- __decorate$3([
- operation
- ], BatchNormOps, "batchNormalization3d", null);
- __decorate$3([
- operation
- ], BatchNormOps, "batchNormalization4d", null);
- __decorate$3([
- doc({ heading: 'Operations', subheading: 'Normalization' })
- ], BatchNormOps, "batchNormalization", null);
- return BatchNormOps;
- }());
- function batchnormReshape4D(x) {
- if (x == null) {
- return null;
- }
- if (x.rank === 0) {
- return x.as1D();
- }
- else if (x.rank === 1) {
- return x;
- }
- else if (x.rank === 2) {
- return x.as4D(1, 1, x.shape[0], x.shape[1]);
- }
- else if (x.rank === 3) {
- return x.as4D(1, x.shape[0], x.shape[1], x.shape[2]);
- }
- return x;
- }
-
- var DType;
- (function (DType) {
- DType["float32"] = "float32";
- DType["int32"] = "int32";
- DType["bool"] = "bool";
- })(DType || (DType = {}));
- var Rank;
- (function (Rank) {
- Rank["R0"] = "R0";
- Rank["R1"] = "R1";
- Rank["R2"] = "R2";
- Rank["R3"] = "R3";
- Rank["R4"] = "R4";
- })(Rank || (Rank = {}));
- var UpcastInt32AndMap;
- (function (UpcastInt32AndMap) {
- UpcastInt32AndMap["float32"] = "float32";
- UpcastInt32AndMap["int32"] = "int32";
- UpcastInt32AndMap["bool"] = "int32";
- })(UpcastInt32AndMap || (UpcastInt32AndMap = {}));
- var UpcastBoolAndMap;
- (function (UpcastBoolAndMap) {
- UpcastBoolAndMap["float32"] = "float32";
- UpcastBoolAndMap["int32"] = "int32";
- UpcastBoolAndMap["bool"] = "bool";
- })(UpcastBoolAndMap || (UpcastBoolAndMap = {}));
- var UpcastFloat32AndMap;
- (function (UpcastFloat32AndMap) {
- UpcastFloat32AndMap["float32"] = "float32";
- UpcastFloat32AndMap["int32"] = "float32";
- UpcastFloat32AndMap["bool"] = "float32";
- })(UpcastFloat32AndMap || (UpcastFloat32AndMap = {}));
- var upcastTypeMap = {
- float32: UpcastFloat32AndMap,
- int32: UpcastInt32AndMap,
- bool: UpcastBoolAndMap
- };
- function upcastType(typeA, typeB) {
- return upcastTypeMap[typeA][typeB];
- }
- function sumOutType(type) {
- return upcastType(type, 'int32');
- }
-
- var __decorate$4 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var BinaryOps = (function () {
- function BinaryOps() {
- }
- BinaryOps.add = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'add');
- assertTypesMatch(a, b);
- var outShape = assertAndGetBroadcastShape(a.shape, b.shape);
- var der = function (dy) {
- var derA = function () {
- var res = dy;
- var reduceAxes = getReductionAxes(a.shape, outShape);
- if (reduceAxes.length > 0) {
- res = res.sum(reduceAxes);
- }
- return res.reshape(a.shape);
- };
- var derB = function () {
- var res = dy;
- var reduceAxes = getReductionAxes(b.shape, outShape);
- if (reduceAxes.length > 0) {
- res = res.sum(reduceAxes);
- }
- return res.reshape(b.shape);
- };
- return { a: derA, b: derB };
- };
- return ENV.engine.runKernel(function (backend) { return backend.add(a, b); }, { a: a, b: b }, der);
- };
- BinaryOps.addStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in addStrict: ');
- return a.add(b);
- };
- BinaryOps.sub = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'sub');
- assertTypesMatch(a, b);
- var outShape = assertAndGetBroadcastShape(a.shape, b.shape);
- var der = function (dy) {
- var derA = function () {
- var res = dy;
- var reduceAxes = getReductionAxes(a.shape, outShape);
- if (reduceAxes.length > 0) {
- res = res.sum(reduceAxes);
- }
- return res.reshape(a.shape);
- };
- var derB = function () {
- var res = dy;
- var reduceAxes = getReductionAxes(b.shape, outShape);
- if (reduceAxes.length > 0) {
- res = res.sum(reduceAxes);
- }
- return res.neg().reshape(b.shape);
- };
- return { a: derA, b: derB };
- };
- return ENV.engine.runKernel(function (backend) { return backend.subtract(a, b); }, { a: a, b: b }, der);
- };
- BinaryOps.subStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in subStrict: ');
- return a.sub(b);
- };
- BinaryOps.pow = function (base, exp$$1) {
- assertArgumentsAreTensors({ base: base, exp: exp$$1 }, 'pow');
- var outShape = assertAndGetBroadcastShape(base.shape, exp$$1.shape);
- base = base.cast(upcastType(base.dtype, exp$$1.dtype));
- exp$$1 = exp$$1.cast(upcastType(base.dtype, exp$$1.dtype));
- var grad = function (dy, saved) {
- var y = saved[0];
- var derBase = function () {
- var res = dy.mul(exp$$1.toFloat().mul(y.div(base)));
- var reduceAxes = getReductionAxes(base.shape, outShape);
- if (reduceAxes.length > 0) {
- res = res.sum(reduceAxes);
- }
- return res.reshape(base.shape);
- };
- var derExp = function () {
- var res = dy.mul(y.mul(base.log()).toFloat());
- var reduceAxes = getReductionAxes(exp$$1.shape, outShape);
- if (reduceAxes.length > 0) {
- res = res.sum(reduceAxes);
- }
- return res.reshape(exp$$1.shape);
- };
- return { base: derBase, exp: derExp };
- };
- return ENV.engine.runKernel(function (backend, save) { return save(backend.pow(base, exp$$1)); }, { base: base, exp: exp$$1 }, grad);
- };
- BinaryOps.powStrict = function (base, exp$$1) {
- assertShapesMatch(base.shape, exp$$1.shape, 'Error in powStrict: ');
- return base.pow(exp$$1);
- };
- BinaryOps.mul = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'mul');
- assertTypesMatch(a, b);
- var outShape = assertAndGetBroadcastShape(a.shape, b.shape);
- var der = function (dy) {
- var derA = function () {
- var res = dy.mul(b.toFloat());
- var reduceAxes = getReductionAxes(a.shape, outShape);
- if (reduceAxes.length > 0) {
- return res.sum(reduceAxes).reshape(a.shape);
- }
- return res;
- };
- var derB = function () {
- var res = dy.mul(a.toFloat());
- var reduceAxes = getReductionAxes(b.shape, outShape);
- if (reduceAxes.length > 0) {
- return res.sum(reduceAxes).reshape(b.shape);
- }
- return res;
- };
- return { a: derA, b: derB };
- };
- return ENV.engine.runKernel(function (backend) { return backend.multiply(a, b); }, { a: a, b: b }, der);
- };
- BinaryOps.mulStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in multiplyStrict: ');
- return a.mul(b);
- };
- BinaryOps.div = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'div');
- assertTypesMatch(a, b);
- var outShape = assertAndGetBroadcastShape(a.shape, b.shape);
- var der = function (dy) {
- var derA = function () {
- var res = dy.div(b.toFloat());
- var reduceAxes = getReductionAxes(a.shape, outShape);
- if (reduceAxes.length > 0) {
- return res.sum(reduceAxes).reshape(a.shape);
- }
- return res;
- };
- var derB = function () {
- var res = dy.mul(a.toFloat());
- var reduceAxes = getReductionAxes(b.shape, outShape);
- if (reduceAxes.length > 0) {
- res = res.sum(reduceAxes).reshape(b.shape);
- }
- var tmp = b.square();
- return res.div(tmp.toFloat()).neg();
- };
- return { a: derA, b: derB };
- };
- return ENV.engine.runKernel(function (backend) { return backend.divide(a, b); }, { a: a, b: b }, der);
- };
- BinaryOps.divStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in divideStrict: ');
- return a.div(b);
- };
- BinaryOps.mod = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'mod');
- assertTypesMatch(a, b);
- var outShape = assertAndGetBroadcastShape(a.shape, b.shape);
- var der = function (dy) {
- var derA = function () {
- var reduceAxes = getReductionAxes(a.shape, outShape);
- if (reduceAxes.length > 0) {
- return dy.sum(reduceAxes).reshape(a.shape);
- }
- return dy;
- };
- var derB = function () {
- var res = dy.mul(a.div(b).floor().neg());
- var reduceAxes = getReductionAxes(b.shape, outShape);
- if (reduceAxes.length > 0) {
- return res.sum(reduceAxes).reshape(b.shape);
- }
- return res;
- };
- return { a: derA, b: derB };
- };
- return ENV.engine.runKernel(function (backend) { return backend.mod(a, b); }, { a: a, b: b }, der);
- };
- BinaryOps.modStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in modStrict: ');
- return a.mod(b);
- };
- BinaryOps.minimum = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'minimum');
- assertTypesMatch(a, b);
- if (a.dtype === 'bool') {
- a = a.toInt();
- }
- if (b.dtype === 'bool') {
- b = b.toInt();
- }
- assertAndGetBroadcastShape(a.shape, b.shape);
- var der = function (dy) {
- var derA = function () { return dy.mul(a.lessEqual(b).toFloat()); };
- var derB = function () { return dy.mul(a.greater(b).toFloat()); };
- return { a: derA, b: derB };
- };
- return ENV.engine.runKernel(function (backend) { return backend.minimum(a, b); }, { a: a, b: b }, der);
- };
- BinaryOps.minimumStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in minimumStrict: ');
- return a.minimum(b);
- };
- BinaryOps.maximum = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'maximum');
- assertTypesMatch(a, b);
- if (a.dtype === 'bool') {
- a = a.toInt();
- }
- if (b.dtype === 'bool') {
- b = b.toInt();
- }
- assertAndGetBroadcastShape(a.shape, b.shape);
- var der = function (dy) {
- var derA = function () { return dy.mul(a.greaterEqual(b).toFloat()); };
- var derB = function () { return dy.mul(a.less(b).toFloat()); };
- return { a: derA, b: derB };
- };
- return ENV.engine.runKernel(function (backend) { return backend.maximum(a, b); }, { a: a, b: b }, der);
- };
- BinaryOps.maximumStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in minimumStrict: ');
- return a.maximum(b);
- };
- BinaryOps.squaredDifference = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'squaredDifference');
- assertTypesMatch(a, b);
- assertAndGetBroadcastShape(a.shape, b.shape);
- var der = function (dy) {
- var two = scalar(2);
- var derA = function () { return dy.mul(a.sub(b).mul(two)); };
- var derB = function () { return dy.mul(b.sub(a).mul(two)); };
- return { a: derA, b: derB };
- };
- return ENV.engine.runKernel(function (backend) { return backend.squaredDifference(a, b); }, { a: a, b: b }, der);
- };
- BinaryOps.squaredDifferenceStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in squaredDifferenceStrict: ');
- return a.squaredDifference(b);
- };
- BinaryOps.atan2 = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'atan2');
- assertTypesMatch(a, b);
- var outShape = assertAndGetBroadcastShape(a.shape, b.shape);
- var der = function (dy) {
- var derA = function () {
- var d = BinaryOps.add(square(a), square(b));
- var res = dy.mul(b.div(d));
- var reduceAxes = getReductionAxes(a.shape, outShape);
- if (reduceAxes.length > 0) {
- res = res.sum(reduceAxes);
- }
- return res.reshape(a.shape);
- };
- var derB = function () {
- var d = BinaryOps.add(square(a), square(b));
- var res = neg(dy.mul(a.div(d)));
- var reduceAxes = getReductionAxes(b.shape, outShape);
- if (reduceAxes.length > 0) {
- res = res.sum(reduceAxes);
- }
- return res.reshape(b.shape);
- };
- return { a: derA, b: derB };
- };
- return ENV.engine.runKernel(function (backend) { return backend.atan2(a, b); }, { a: a, b: b }, der);
- };
- __decorate$4([
- doc({ heading: 'Operations', subheading: 'Arithmetic' }),
- operation
- ], BinaryOps, "add", null);
- __decorate$4([
- operation
- ], BinaryOps, "addStrict", null);
- __decorate$4([
- doc({ heading: 'Operations', subheading: 'Arithmetic' }),
- operation
- ], BinaryOps, "sub", null);
- __decorate$4([
- operation
- ], BinaryOps, "subStrict", null);
- __decorate$4([
- doc({ heading: 'Operations', subheading: 'Arithmetic' }),
- operation
- ], BinaryOps, "pow", null);
- __decorate$4([
- operation
- ], BinaryOps, "powStrict", null);
- __decorate$4([
- doc({ heading: 'Operations', subheading: 'Arithmetic' }),
- operation
- ], BinaryOps, "mul", null);
- __decorate$4([
- operation
- ], BinaryOps, "mulStrict", null);
- __decorate$4([
- doc({ heading: 'Operations', subheading: 'Arithmetic' }),
- operation
- ], BinaryOps, "div", null);
- __decorate$4([
- operation
- ], BinaryOps, "divStrict", null);
- __decorate$4([
- doc({ heading: 'Operations', subheading: 'Arithmetic' }),
- operation
- ], BinaryOps, "mod", null);
- __decorate$4([
- operation
- ], BinaryOps, "modStrict", null);
- __decorate$4([
- doc({ heading: 'Operations', subheading: 'Arithmetic' }),
- operation
- ], BinaryOps, "minimum", null);
- __decorate$4([
- operation
- ], BinaryOps, "minimumStrict", null);
- __decorate$4([
- doc({ heading: 'Operations', subheading: 'Arithmetic' }),
- operation
- ], BinaryOps, "maximum", null);
- __decorate$4([
- operation
- ], BinaryOps, "maximumStrict", null);
- __decorate$4([
- doc({ heading: 'Operations', subheading: 'Arithmetic' }),
- operation
- ], BinaryOps, "squaredDifference", null);
- __decorate$4([
- operation
- ], BinaryOps, "squaredDifferenceStrict", null);
- __decorate$4([
- operation
- ], BinaryOps, "atan2", null);
- return BinaryOps;
- }());
-
- var __decorate$5 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var CompareOps = (function () {
- function CompareOps() {
- }
- CompareOps.notEqual = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'notEqual');
- assertTypesMatch(a, b);
- assertAndGetBroadcastShape(a.shape, b.shape);
- return ENV.engine.runKernel(function (backend) { return backend.notEqual(a, b); }, { a: a, b: b });
- };
- CompareOps.notEqualStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in notEqualStrict: ');
- return a.notEqual(b);
- };
- CompareOps.less = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'less');
- assertTypesMatch(a, b);
- assertAndGetBroadcastShape(a.shape, b.shape);
- return ENV.engine.runKernel(function (backend) { return backend.less(a, b); }, { a: a, b: b });
- };
- CompareOps.lessStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in lessStrict: ');
- return a.less(b);
- };
- CompareOps.equal = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'equal');
- assertTypesMatch(a, b);
- assertAndGetBroadcastShape(a.shape, b.shape);
- return ENV.engine.runKernel(function (backend) { return backend.equal(a, b); }, { a: a, b: b });
- };
- CompareOps.equalStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in equalStrict: ');
- return a.equal(b);
- };
- CompareOps.lessEqual = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'lessEqual');
- assertTypesMatch(a, b);
- assertAndGetBroadcastShape(a.shape, b.shape);
- return ENV.engine.runKernel(function (backend) { return backend.lessEqual(a, b); }, { a: a, b: b });
- };
- CompareOps.lessEqualStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in lessEqualStrict: ');
- return a.lessEqual(b);
- };
- CompareOps.greater = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'greater');
- assertTypesMatch(a, b);
- assertAndGetBroadcastShape(a.shape, b.shape);
- return ENV.engine.runKernel(function (backend) { return backend.greater(a, b); }, { a: a, b: b });
- };
- CompareOps.greaterStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in greaterStrict: ');
- return a.greater(b);
- };
- CompareOps.greaterEqual = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'greaterEqual');
- assertTypesMatch(a, b);
- assertAndGetBroadcastShape(a.shape, b.shape);
- return ENV.engine.runKernel(function (backend) { return backend.greaterEqual(a, b); }, { a: a, b: b });
- };
- CompareOps.greaterEqualStrict = function (a, b) {
- assertShapesMatch(a.shape, b.shape, 'Error in greaterEqualStrict: ');
- return a.greaterEqual(b);
- };
- __decorate$5([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], CompareOps, "notEqual", null);
- __decorate$5([
- operation
- ], CompareOps, "notEqualStrict", null);
- __decorate$5([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], CompareOps, "less", null);
- __decorate$5([
- operation
- ], CompareOps, "lessStrict", null);
- __decorate$5([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], CompareOps, "equal", null);
- __decorate$5([
- operation
- ], CompareOps, "equalStrict", null);
- __decorate$5([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], CompareOps, "lessEqual", null);
- __decorate$5([
- operation
- ], CompareOps, "lessEqualStrict", null);
- __decorate$5([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], CompareOps, "greater", null);
- __decorate$5([
- operation
- ], CompareOps, "greaterStrict", null);
- __decorate$5([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], CompareOps, "greaterEqual", null);
- __decorate$5([
- operation
- ], CompareOps, "greaterEqualStrict", null);
- return CompareOps;
- }());
-
- function computePool2DInfo(inShape, filterSize, strides, pad, roundingMode, dataFormat) {
- if (dataFormat === void 0) { dataFormat = 'channelsLast'; }
- var _a = parseTupleParam(filterSize), filterHeight = _a[0], filterWidth = _a[1];
- var filterShape;
- if (dataFormat === 'channelsLast') {
- filterShape = [filterHeight, filterWidth, inShape[3], inShape[3]];
- }
- else if (dataFormat === 'channelsFirst') {
- filterShape = [filterHeight, filterWidth, inShape[1], inShape[1]];
- }
- else {
- throw new Error("Unknown dataFormat " + dataFormat);
- }
- var dilations = 1;
- return computeConv2DInfo(inShape, filterShape, strides, dilations, pad, roundingMode, false, dataFormat);
- }
- function computeConv2DInfo(inShape, filterShape, strides, dilations, pad, roundingMode, depthwise, dataFormat) {
- if (depthwise === void 0) { depthwise = false; }
- if (dataFormat === void 0) { dataFormat = 'channelsLast'; }
- var _a = [-1, -1, -1, -1], batchSize = _a[0], inHeight = _a[1], inWidth = _a[2], inChannels = _a[3];
- if (dataFormat === 'channelsLast') {
- batchSize = inShape[0], inHeight = inShape[1], inWidth = inShape[2], inChannels = inShape[3];
- }
- else if (dataFormat === 'channelsFirst') {
- batchSize = inShape[0], inChannels = inShape[1], inHeight = inShape[2], inWidth = inShape[3];
- }
- else {
- throw new Error("Unknown dataFormat " + dataFormat);
- }
- var filterHeight = filterShape[0], filterWidth = filterShape[1], filterChannels = filterShape[3];
- var _b = parseTupleParam(strides), strideHeight = _b[0], strideWidth = _b[1];
- var _c = parseTupleParam(dilations), dilationHeight = _c[0], dilationWidth = _c[1];
- var effectiveFilterHeight = getEffectiveFilterSize(filterHeight, dilationHeight);
- var effectiveFilterWidth = getEffectiveFilterSize(filterWidth, dilationWidth);
- var _d = getPadAndOutInfo(pad, inHeight, inWidth, strideHeight, strideWidth, effectiveFilterHeight, effectiveFilterWidth, roundingMode), padInfo = _d.padInfo, outHeight = _d.outHeight, outWidth = _d.outWidth;
- var outChannels = depthwise ? filterChannels * inChannels : filterChannels;
- var outShape;
- if (dataFormat === 'channelsFirst') {
- outShape = [batchSize, outChannels, outHeight, outWidth];
- }
- else if (dataFormat === 'channelsLast') {
- outShape = [batchSize, outHeight, outWidth, outChannels];
- }
- return {
- batchSize: batchSize,
- dataFormat: dataFormat,
- inHeight: inHeight,
- inWidth: inWidth,
- inChannels: inChannels,
- outHeight: outHeight,
- outWidth: outWidth,
- outChannels: outChannels,
- padInfo: padInfo,
- strideHeight: strideHeight,
- strideWidth: strideWidth,
- filterHeight: filterHeight,
- filterWidth: filterWidth,
- dilationHeight: dilationHeight,
- dilationWidth: dilationWidth,
- inShape: inShape,
- outShape: outShape,
- filterShape: filterShape
- };
- }
- function computeOutputShape3D(inShape, fieldSize, outDepth, stride, zeroPad, roundingMode) {
- if (zeroPad == null) {
- zeroPad = computeDefaultPad(inShape, fieldSize, stride);
- }
- var inputRows = inShape[0];
- var inputCols = inShape[1];
- var outputRows = conditionalRound((inputRows - fieldSize + 2 * zeroPad) / stride + 1, roundingMode);
- assert(isInt(outputRows), "The output # of rows (" + outputRows + ") must be an integer. Change the " +
- "stride and/or zero pad parameters");
- var outputCols = conditionalRound((inputCols - fieldSize + 2 * zeroPad) / stride + 1, roundingMode);
- assert(isInt(outputCols), "The output # of columns (" + outputCols + ") must be an integer. Change " +
- "the stride and/or zero pad parameters");
- return [outputRows, outputCols, outDepth];
- }
- function computeDefaultPad(inputShape, fieldSize, stride, dilation) {
- if (dilation === void 0) { dilation = 1; }
- var effectiveFieldSize = getEffectiveFilterSize(fieldSize, dilation);
- return Math.floor((inputShape[0] * (stride - 1) - stride + effectiveFieldSize) / 2);
- }
- function parseTupleParam(param) {
- return typeof param === 'number' ? [param, param] : param;
- }
- function getEffectiveFilterSize(filterSize, dilation) {
- if (dilation <= 1) {
- return filterSize;
- }
- return filterSize + (filterSize - 1) * (dilation - 1);
- }
- function getPadAndOutInfo(pad, inHeight, inWidth, strideHeight, strideWidth, filterHeight, filterWidth, roundingMode) {
- var padInfo;
- var outHeight;
- var outWidth;
- if (typeof pad === 'number') {
- var padType = (pad === 0) ? 'VALID' : 'NUMBER';
- padInfo = { top: pad, bottom: pad, left: pad, right: pad, type: padType };
- var outShape = computeOutputShape3D([inHeight, inWidth, 1], filterHeight, 1, strideHeight, pad, roundingMode);
- outHeight = outShape[0];
- outWidth = outShape[1];
- }
- else if (pad === 'same') {
- outHeight = Math.ceil(inHeight / strideHeight);
- outWidth = Math.ceil(inWidth / strideWidth);
- var padAlongHeight = (outHeight - 1) * strideHeight + filterHeight - inHeight;
- var padAlongWidth = (outWidth - 1) * strideWidth + filterWidth - inWidth;
- var top_1 = Math.floor(padAlongHeight / 2);
- var bottom = padAlongHeight - top_1;
- var left = Math.floor(padAlongWidth / 2);
- var right = padAlongWidth - left;
- padInfo = { top: top_1, bottom: bottom, left: left, right: right, type: 'SAME' };
- }
- else if (pad === 'valid') {
- padInfo = { top: 0, bottom: 0, left: 0, right: 0, type: 'VALID' };
- outHeight = Math.ceil((inHeight - filterHeight + 1) / strideHeight);
- outWidth = Math.ceil((inWidth - filterWidth + 1) / strideWidth);
- }
- else {
- throw Error("Unknown padding parameter: " + pad);
- }
- return { padInfo: padInfo, outHeight: outHeight, outWidth: outWidth };
- }
- function conditionalRound(value, roundingMode) {
- if (!roundingMode) {
- return value;
- }
- switch (roundingMode) {
- case 'round':
- return Math.round(value);
- case 'ceil':
- return Math.ceil(value);
- case 'floor':
- return Math.floor(value);
- default:
- throw new Error("Unknown roundingMode " + roundingMode);
- }
- }
-
- var __decorate$6 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var ConvOps = (function () {
- function ConvOps() {
- }
- ConvOps.conv1d = function (x, filter, stride, pad, dataFormat, dilation, dimRoundingMode) {
- if (dataFormat === void 0) { dataFormat = 'NWC'; }
- if (dilation === void 0) { dilation = 1; }
- assertArgumentsAreTensors({ x: x, filter: filter }, 'conv1d');
- var x3D = x;
- var reshapedTo3D = false;
- if (x.rank === 2) {
- reshapedTo3D = true;
- x3D = x.as3D(1, x.shape[0], x.shape[1]);
- }
- assert(x3D.rank === 3, "Error in conv1d: input must be rank 3, but got rank " + x3D.rank + ".");
- assert(filter.rank === 3, "Error in conv1d: filter must be rank 3, but got rank " +
- (filter.rank + "."));
- if (dimRoundingMode != null) {
- assert(isInt(pad), "Error in conv1d: pad must be an integer when using, " +
- ("dimRoundingMode " + dimRoundingMode + " but got pad " + pad + "."));
- }
- assert(x3D.shape[2] === filter.shape[1], "Error in conv1d: depth of input (" + x3D.shape[2] + ") must match " +
- ("input depth for filter " + filter.shape[1] + "."));
- assert(eitherStridesOrDilationsAreOne(stride, dilation), 'Error in conv1D: Either stride or dilation must be 1.' +
- ("Got stride " + stride + " and dilation '" + dilation + "'"));
- assert(dataFormat === 'NWC', "Error in conv1d: got dataFormat of " + dataFormat + " but only NWC is currently supported.");
- var filter4D = filter.as4D(1, filter.shape[0], filter.shape[1], filter.shape[2]);
- var input4D = x3D.as4D(x3D.shape[0], 1, x3D.shape[1], x3D.shape[2]);
- var strides = [1, stride];
- var dilations = [1, dilation];
- var conv2dDataFormat = 'NHWC';
- var res = ConvOps.conv2d(input4D, filter4D, strides, pad, conv2dDataFormat, dilations, dimRoundingMode);
- if (reshapedTo3D) {
- return res.as2D(res.shape[2], res.shape[3]);
- }
- return res.as3D(res.shape[0], res.shape[2], res.shape[3]);
- };
- ConvOps.conv2d = function (x, filter, strides, pad, dataFormat, dilations, dimRoundingMode) {
- if (dataFormat === void 0) { dataFormat = 'NHWC'; }
- if (dilations === void 0) { dilations = [1, 1]; }
- assertArgumentsAreTensors({ x: x, filter: filter }, 'conv2d');
- var x4D = x;
- var reshapedTo4D = false;
- if (x.rank === 3) {
- reshapedTo4D = true;
- x4D = x.as4D(1, x.shape[0], x.shape[1], x.shape[2]);
- }
- assert(x4D.rank === 4, "Error in conv2d: input must be rank 4, but got rank " + x4D.rank + ".");
- assert(filter.rank === 4, "Error in conv2d: filter must be rank 4, but got rank " +
- (filter.rank + "."));
- if (dimRoundingMode != null) {
- assert(isInt(pad), "Error in conv2d: pad must be an integer when using, " +
- ("dimRoundingMode " + dimRoundingMode + " but got pad " + pad + "."));
- }
- assert(x4D.shape[3] === filter.shape[2], "Error in conv2d: depth of input (" + x4D.shape[3] + ") must match " +
- ("input depth for filter " + filter.shape[2] + "."));
- assert(eitherStridesOrDilationsAreOne(strides, dilations), 'Error in conv2D: Either strides or dilations must be 1.' +
- ("Got strides " + strides + " and dilations '" + dilations + "'"));
- assert(dataFormat === 'NHWC', "Error in conv2d: got dataFormat of " + dataFormat + " but only NHWC is currently supported.");
- var convInfo = computeConv2DInfo(x4D.shape, filter.shape, strides, dilations, pad, dimRoundingMode);
- var grad = function (dy) {
- assert(tupleValuesAreOne(dilations), 'Error in gradient of conv2D: dilation rates greater than 1 are not' +
- ("yet supported in gradients. Got dilations '" + dilations + "'"));
- return {
- x: function () { return ConvOps.conv2dDerInput(x4D.shape, dy, filter, strides, pad); },
- filter: function () {
- return ConvOps.conv2dDerFilter(x4D, dy, filter.shape, strides, pad);
- }
- };
- };
- var res = ENV.engine.runKernel(function (backend) { return backend.conv2d(x4D, filter, convInfo); }, { x: x4D, filter: filter }, grad);
- if (reshapedTo4D) {
- return res.as3D(res.shape[1], res.shape[2], res.shape[3]);
- }
- return res;
- };
- ConvOps.conv2dDerInput = function (xShape, dy, filter, strides, pad, dimRoundingMode) {
- assertArgumentsAreTensors({ dy: dy, filter: filter }, 'conv2dDerInput');
- assert(xShape.length === dy.rank, "Length of inShape " +
- ("(" + xShape.length + ") and rank of dy (" + dy.rank + ") must match"));
- var xShape4D = xShape;
- var dy4D = dy;
- var reshapedTo4D = false;
- if (dy.rank === 3) {
- reshapedTo4D = true;
- dy4D = dy.as4D(1, dy.shape[0], dy.shape[1], dy.shape[2]);
- xShape4D = [1, xShape[0], xShape[1], xShape[2]];
- }
- var inDepth = xShape4D[3];
- var outDepth = dy4D.shape[3];
- assert(xShape4D.length === 4, "Error in conv2dDerInput: inShape must be length 4, but got length " +
- (xShape4D.length + "."));
- assert(dy4D.rank === 4, "Error in conv2dDerInput: dy must be rank 4, but got " +
- ("rank " + dy4D.rank));
- assert(filter.rank === 4, "Error in conv2dDerInput: filter must be rank 4, but got " +
- ("rank " + filter.rank));
- assert(inDepth === filter.shape[2], "Error in conv2dDerInput: depth of input (" + inDepth + ") must " +
- ("match input depth for filter " + filter.shape[2] + "."));
- assert(outDepth === filter.shape[3], "Error in conv2dDerInput: depth of output (" + outDepth + ") must" +
- ("match output depth for filter " + filter.shape[3] + "."));
- if (dimRoundingMode != null) {
- assert(isInt(pad), "Error in conv2dDerInput: pad must be an integer when using, " +
- ("dimRoundingMode " + dimRoundingMode + " but got pad " + pad + "."));
- }
- var dilations = 1;
- var convInfo = computeConv2DInfo(xShape4D, filter.shape, strides, dilations, pad, dimRoundingMode);
- var res = ENV.engine.runKernel(function (backend) { return backend.conv2dDerInput(dy4D, filter, convInfo); }, { dy4D: dy4D });
- if (reshapedTo4D) {
- return res.as3D(res.shape[1], res.shape[2], res.shape[3]);
- }
- return res;
- };
- ConvOps.conv2dDerFilter = function (x, dy, filterShape, strides, pad, dimRoundingMode) {
- assertArgumentsAreTensors({ x: x, dy: dy }, 'conv2dDerFilter');
- var x4D = x;
- if (x.rank === 3) {
- x4D = x.as4D(1, x.shape[0], x.shape[1], x.shape[2]);
- }
- var dy4D = dy;
- if (dy4D.rank === 3) {
- dy4D = dy.as4D(1, dy.shape[0], dy.shape[1], dy.shape[2]);
- }
- assert(x4D.rank === 4, "Error in conv2dDerFilter: input must be rank 4, but got shape " +
- (x4D.shape + "."));
- assert(dy4D.rank === 4, "Error in conv2dDerFilter: dy must be rank 4, but got shape " +
- (dy4D.shape + "."));
- assert(filterShape.length === 4, "Error in conv2dDerFilter: filterShape must be length 4, but got " +
- (filterShape + "."));
- assert(x4D.shape[3] === filterShape[2], "Error in conv2dDerFilter: depth of input " + x4D.shape[3] + ") must " +
- ("match input depth in filter (" + filterShape[2] + "."));
- assert(dy4D.shape[3] === filterShape[3], "Error in conv2dDerFilter: depth of dy (" + dy4D.shape[3] + ") must " +
- ("match output depth for filter (" + filterShape[3] + ")."));
- if (dimRoundingMode != null) {
- assert(isInt(pad), "Error in conv2dDerFilter: pad must be an integer when using, " +
- ("dimRoundingMode " + dimRoundingMode + " but got pad " + pad + "."));
- }
- var dilations = 1;
- var convInfo = computeConv2DInfo(x4D.shape, filterShape, strides, dilations, pad, dimRoundingMode);
- return ENV.engine.runKernel(function (backend) { return backend.conv2dDerFilter(x4D, dy4D, convInfo); }, { x4D: x4D, dy4D: dy4D });
- };
- ConvOps.conv2dTranspose = function (x, filter, outputShape, strides, pad, dimRoundingMode) {
- assertArgumentsAreTensors({ x: x, filter: filter }, 'conv2dTranspose');
- return ConvOps.conv2dDerInput(outputShape, x, filter, strides, pad, dimRoundingMode);
- };
- ConvOps.depthwiseConv2d = function (x, filter, strides, pad, dataFormat, dilations, dimRoundingMode) {
- if (dataFormat === void 0) { dataFormat = 'NHWC'; }
- if (dilations === void 0) { dilations = [1, 1]; }
- assertArgumentsAreTensors({ x: x, filter: filter }, 'depthwiseConv2d');
- var x4D = x;
- var reshapedTo4D = false;
- if (x.rank === 3) {
- reshapedTo4D = true;
- x4D = x.as4D(1, x.shape[0], x.shape[1], x.shape[2]);
- }
- assert(x4D.rank === 4, "Error in depthwiseConv2D: input must be rank 4, but got " +
- ("rank " + x4D.rank + "."));
- assert(filter.rank === 4, "Error in depthwiseConv2D: filter must be rank 4, but got rank " +
- (filter.rank + "."));
- assert(x4D.shape[3] === filter.shape[2], "Error in depthwiseConv2D: number of input channels " +
- ("(" + x4D.shape[3] + ") must match the inChannels dimension in ") +
- ("filter " + filter.shape[2] + "."));
- if (dilations == null) {
- dilations = [1, 1];
- }
- assert(eitherStridesOrDilationsAreOne(strides, dilations), 'Error in depthwiseConv2d: Either strides or dilations must be 1.' +
- ("Got strides " + strides + " and dilations '" + dilations + "'"));
- if (dimRoundingMode != null) {
- assert(isInt(pad), "Error in depthwiseConv2D: pad must be an integer when using, " +
- ("dimRoundingMode " + dimRoundingMode + " but got pad " + pad + "."));
- }
- var convInfo = computeConv2DInfo(x4D.shape, filter.shape, strides, dilations, pad, dimRoundingMode, true);
- var res = ENV.engine.runKernel(function (backend) { return backend.depthwiseConv2D(x4D, filter, convInfo); }, { x4D: x4D, filter: filter });
- if (reshapedTo4D) {
- return res.as3D(res.shape[1], res.shape[2], res.shape[3]);
- }
- return res;
- };
- ConvOps.separableConv2d = function (x, depthwiseFilter, pointwiseFilter, strides, pad, dilation, dataFormat) {
- if (dilation === void 0) { dilation = [1, 1]; }
- if (dataFormat === void 0) { dataFormat = 'NHWC'; }
- assertArgumentsAreTensors({ x: x, depthwiseFilter: depthwiseFilter, pointwiseFilter: pointwiseFilter }, 'separableConv2d');
- var x4D = x;
- var reshapedTo4D = false;
- if (x.rank === 3) {
- reshapedTo4D = true;
- x4D = x.as4D(1, x.shape[0], x.shape[1], x.shape[2]);
- }
- if (dataFormat === 'NCHW') {
- throw new Error('separableConv2d currently does not support dataFormat NCHW; only ' +
- 'NHWC is supported');
- }
- assert(x4D.rank === 4, "Error in separableConv2d: input must be rank 4, but got " +
- ("rank " + x4D.rank + "."));
- assert(depthwiseFilter.rank === 4, "Error in separableConv2d: depthwise filter must be rank 4, but got " +
- ("rank " + depthwiseFilter.rank + "."));
- assert(pointwiseFilter.rank === 4, "Error in separableConv2d: pointwise filter must be rank 4, but got " +
- ("rank " + depthwiseFilter.rank + "."));
- assert(pointwiseFilter.shape[0] === 1, "Error in separableConv2d: the first dimension of pointwise filter " +
- (" must be 1, but got " + pointwiseFilter.shape[0] + "."));
- assert(pointwiseFilter.shape[1] === 1, "Error in separableConv2d: the second dimension of pointwise filter " +
- (" must be 1, but got " + pointwiseFilter.shape[1] + "."));
- var inChannels = depthwiseFilter.shape[2];
- var channelMultiplier = depthwiseFilter.shape[3];
- assert(pointwiseFilter.shape[2] === inChannels * channelMultiplier, "Error in separableConv2d: the third dimension of pointwise filter " +
- ("must be " + inChannels * channelMultiplier + ", ") +
- ("but got " + pointwiseFilter.shape[2] + "."));
- var depthwise = ConvOps.depthwiseConv2d(x4D, depthwiseFilter, strides, pad, dataFormat, dilation);
- var pointwiseStride = 1;
- var res = ConvOps.conv2d(depthwise, pointwiseFilter, pointwiseStride, 'valid', dataFormat);
- if (reshapedTo4D) {
- return res.as3D(res.shape[1], res.shape[2], res.shape[3]);
- }
- return res;
- };
- __decorate$6([
- doc({ heading: 'Operations', subheading: 'Convolution' }),
- operation
- ], ConvOps, "conv1d", null);
- __decorate$6([
- doc({ heading: 'Operations', subheading: 'Convolution' }),
- operation
- ], ConvOps, "conv2d", null);
- __decorate$6([
- operation
- ], ConvOps, "conv2dDerInput", null);
- __decorate$6([
- operation
- ], ConvOps, "conv2dDerFilter", null);
- __decorate$6([
- doc({ heading: 'Operations', subheading: 'Convolution' }),
- operation
- ], ConvOps, "conv2dTranspose", null);
- __decorate$6([
- doc({ heading: 'Operations', subheading: 'Convolution' }),
- operation
- ], ConvOps, "depthwiseConv2d", null);
- __decorate$6([
- doc({ heading: 'Operations', subheading: 'Convolution' }),
- operation
- ], ConvOps, "separableConv2d", null);
- return ConvOps;
- }());
- function parseTupleParam$1(param) {
- return typeof param === 'number' ? [param, param] : param;
- }
- function tupleValuesAreOne(param) {
- var _a = parseTupleParam$1(param), dimA = _a[0], dimB = _a[1];
- return dimA === 1 && dimB === 1;
- }
- function eitherStridesOrDilationsAreOne(strides, dilations) {
- return tupleValuesAreOne(strides) || tupleValuesAreOne(dilations);
- }
-
- var __decorate$7 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var ImageOps = (function () {
- function ImageOps() {
- }
- ImageOps.resizeBilinear = function (images, size, alignCorners) {
- if (alignCorners === void 0) { alignCorners = false; }
- assertArgumentsAreTensors({ images: images }, 'resizeBilinear');
- assert(images.rank === 3 || images.rank === 4, "Error in resizeBilinear: x must be rank 3 or 4, but got " +
- ("rank " + images.rank + "."));
- assert(size.length === 2, "Error in resizeBilinear: new shape must 2D, but got shape " +
- (size + "."));
- var batchImages = images;
- var reshapedTo4D = false;
- if (images.rank === 3) {
- reshapedTo4D = true;
- batchImages =
- images.as4D(1, images.shape[0], images.shape[1], images.shape[2]);
- }
- var newHeight = size[0], newWidth = size[1];
- var forward = function (backend, save) {
- return backend.resizeBilinear(batchImages, newHeight, newWidth, alignCorners);
- };
- var backward = function (dy, saved) {
- return {
- batchImages: function () { return ENV.engine.runKernel(function (backend) {
- return backend.resizeBilinearBackprop(dy, batchImages, alignCorners);
- }, {}); }
- };
- };
- var res = ENV.engine.runKernel(forward, { batchImages: batchImages }, backward);
- if (reshapedTo4D) {
- return res.as3D(res.shape[1], res.shape[2], res.shape[3]);
- }
- return res;
- };
- ImageOps.resizeNearestNeighbor = function (images, size, alignCorners) {
- if (alignCorners === void 0) { alignCorners = false; }
- assertArgumentsAreTensors({ images: images }, 'resizeNearestNeighbor');
- assert(images.rank === 3 || images.rank === 4, "Error in resizeNearestNeighbor: x must be rank 3 or 4, but got " +
- ("rank " + images.rank + "."));
- assert(size.length === 2, "Error in resizeNearestNeighbor: new shape must 2D, but got shape " +
- (size + "."));
- assert(images.dtype === 'float32' || images.dtype === 'int32', '`images` must have `int32` or `float32` as dtype');
- var batchImages = images;
- var reshapedTo4D = false;
- if (images.rank === 3) {
- reshapedTo4D = true;
- batchImages =
- images.as4D(1, images.shape[0], images.shape[1], images.shape[2]);
- }
- var newHeight = size[0], newWidth = size[1];
- var res = ENV.engine.runKernel(function (backend) { return backend.resizeNearestNeighbor(batchImages, newHeight, newWidth, alignCorners); }, { batchImages: batchImages });
- if (reshapedTo4D) {
- return res.as3D(res.shape[1], res.shape[2], res.shape[3]);
- }
- return res;
- };
- __decorate$7([
- doc({ heading: 'Operations', subheading: 'Images', namespace: 'image' }),
- operation
- ], ImageOps, "resizeBilinear", null);
- __decorate$7([
- doc({ heading: 'Operations', subheading: 'Images', namespace: 'image' }),
- operation
- ], ImageOps, "resizeNearestNeighbor", null);
- return ImageOps;
- }());
-
- var __decorate$8 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var Tracking = (function () {
- function Tracking() {
- }
- Tracking.tidy = function (nameOrFn, fn, gradMode) {
- if (gradMode === void 0) { gradMode = false; }
- var name = null;
- if (fn == null) {
- if (typeof nameOrFn !== 'function') {
- throw new Error('Please provide a function to tidy()');
- }
- fn = nameOrFn;
- }
- else {
- if (typeof nameOrFn !== 'string' && !(nameOrFn instanceof String)) {
- throw new Error('When calling with two arguments, the first argument ' +
- 'to tidy() must be a string');
- }
- if (typeof fn !== 'function') {
- throw new Error('When calling with two arguments, the 2nd argument ' +
- 'to tidy() must be a function');
- }
- name = nameOrFn;
- }
- ENV.engine.startScope(name, gradMode);
- var result = fn();
- if (result instanceof Promise) {
- console.error('Cannot return a Promise inside of tidy.');
- }
- ENV.engine.endScope(result, gradMode);
- return result;
- };
- Tracking.dispose = function (container) {
- var tensors = extractTensorsFromAny(container);
- tensors.forEach(function (tensor) { return tensor.dispose(); });
- };
- Tracking.keep = function (result) {
- return ENV.engine.keep(result);
- };
- Tracking.time = function (f) {
- return ENV.engine.time(f);
- };
- __decorate$8([
- doc({ heading: 'Performance', subheading: 'Memory' })
- ], Tracking, "tidy", null);
- __decorate$8([
- doc({ heading: 'Performance', subheading: 'Memory' })
- ], Tracking, "keep", null);
- __decorate$8([
- doc({ heading: 'Performance', subheading: 'Timing' })
- ], Tracking, "time", null);
- return Tracking;
- }());
-
- var __decorate$9 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var LinalgOps = (function () {
- function LinalgOps() {
- }
- LinalgOps.gramSchmidt = function (xs) {
- var inputIsTensor2D;
- if (Array.isArray(xs)) {
- inputIsTensor2D = false;
- assert(xs != null && xs.length > 0, 'Gram-Schmidt process: input must not be null, undefined, or empty');
- var dim = xs[0].shape[0];
- for (var i = 1; i < xs.length; ++i) {
- assert(xs[i].shape[0] === dim, 'Gram-Schmidt: Non-unique lengths found in the input vectors: ' +
- ("(" + xs[i].shape[0] + " vs. " + dim + ")"));
- }
- }
- else {
- inputIsTensor2D = true;
- xs = split(xs, xs.shape[0], 0).map(function (x) { return squeeze(x, [0]); });
- }
- assert(xs.length <= xs[0].shape[0], "Gram-Schmidt: Number of vectors (" + xs.length + ") exceeds " +
- ("number of dimensions (" + xs[0].shape[0] + ")."));
- var ys = [];
- var xs1d = xs;
- var _loop_1 = function (i) {
- ys.push(Tracking.tidy(function () {
- var x = xs1d[i];
- if (i > 0) {
- for (var j = 0; j < i; ++j) {
- var proj = sum(ys[j].mulStrict(x)).mul(ys[j]);
- x = x.sub(proj);
- }
- }
- return x.div(norm(x, 'euclidean'));
- }));
- };
- for (var i = 0; i < xs.length; ++i) {
- _loop_1(i);
- }
- if (inputIsTensor2D) {
- return stack(ys, 0);
- }
- else {
- return ys;
- }
- };
- __decorate$9([
- doc({ heading: 'Operations', subheading: 'Linear Algebra' }),
- operation
- ], LinalgOps, "gramSchmidt", null);
- return LinalgOps;
- }());
-
- var __decorate$a = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var LogicalOps = (function () {
- function LogicalOps() {
- }
- LogicalOps.logicalNot = function (x) {
- assertArgumentsAreTensors({ x: x }, 'logicalNot');
- assert(x.dtype === 'bool', 'Error Array must be of type bool.');
- return ENV.engine.runKernel(function (backend) { return backend.logicalNot(x); }, { x: x });
- };
- LogicalOps.logicalAnd = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'logicalAnd');
- assert(a.dtype === 'bool' && b.dtype === 'bool', 'Error Array must be of type bool.');
- assertAndGetBroadcastShape(a.shape, b.shape);
- return ENV.engine.runKernel(function (backend) { return backend.logicalAnd(a, b); }, { a: a, b: b });
- };
- LogicalOps.logicalOr = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'logicalOr');
- assert(a.dtype === 'bool' && b.dtype === 'bool', 'Error Array must be of type bool.');
- assertAndGetBroadcastShape(a.shape, b.shape);
- return ENV.engine.runKernel(function (backend) { return backend.logicalOr(a, b); }, { a: a, b: b });
- };
- LogicalOps.logicalXor = function (a, b) {
- assertArgumentsAreTensors({ a: a, b: b }, 'logicalXor');
- assert(a.dtype === 'bool' && b.dtype === 'bool', 'Error Array must be of type bool.');
- assertAndGetBroadcastShape(a.shape, b.shape);
- return LogicalOps.logicalOr(a, b).logicalAnd(LogicalOps.logicalAnd(a, b).logicalNot());
- };
- LogicalOps.where = function (condition, a, b) {
- assertArgumentsAreTensors({ condition: condition, a: a, b: b }, 'where');
- assert(condition.dtype === 'bool' || a.dtype === 'bool' || b.dtype === 'bool', 'Error Array must be of type bool.');
- assertShapesMatch(a.shape, b.shape, 'Error in where: ');
- if (condition.rank === 1) {
- assert(condition.shape[0] === a.shape[0], 'The first dimension of `a` must match the size of `condition`.');
- }
- else {
- assertShapesMatch(condition.shape, b.shape, 'Error in where: ');
- }
- var dtype = upcastType(a.dtype, b.dtype);
- return ENV.engine.runKernel(function (backend) { return backend.where(condition, a, b, dtype); }, { condition: condition, a: a, b: b });
- };
- __decorate$a([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], LogicalOps, "logicalNot", null);
- __decorate$a([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], LogicalOps, "logicalAnd", null);
- __decorate$a([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], LogicalOps, "logicalOr", null);
- __decorate$a([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], LogicalOps, "logicalXor", null);
- __decorate$a([
- doc({ heading: 'Operations', subheading: 'Logical' }),
- operation
- ], LogicalOps, "where", null);
- return LogicalOps;
- }());
-
- var __decorate$b = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var Reduction;
- (function (Reduction) {
- Reduction[Reduction["NONE"] = 0] = "NONE";
- Reduction[Reduction["MEAN"] = 1] = "MEAN";
- Reduction[Reduction["SUM"] = 2] = "SUM";
- Reduction[Reduction["SUM_BY_NONZERO_WEIGHTS"] = 3] = "SUM_BY_NONZERO_WEIGHTS";
- })(Reduction || (Reduction = {}));
- var LossOps = (function () {
- function LossOps() {
- }
- LossOps.computeWeightedLoss = function (losses$$1, weights, reduction) {
- if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
- assertArgumentsAreTensors({ losses: losses$$1 }, 'computeWeightedLoss');
- if (weights != null) {
- assertArgumentsAreTensors({ weights: weights }, 'computeWeightedLoss');
- }
- var weightedLoss = (weights == null) ? losses$$1 : losses$$1.mul(weights);
- if (reduction === Reduction.NONE) {
- return weightedLoss;
- }
- if (reduction === Reduction.SUM) {
- return weightedLoss.sum();
- }
- if (reduction === Reduction.MEAN) {
- return (weights == null) ? weightedLoss.mean() :
- weightedLoss.sum().div(weights.sum());
- }
- if (reduction === Reduction.SUM_BY_NONZERO_WEIGHTS) {
- if (weights == null) {
- return weightedLoss.sum().div(scalar(losses$$1.size));
- }
- else {
- var numNonZeros = weights.notEqual(scalar(0)).sum().toFloat();
- return weightedLoss.sum().div(numNonZeros);
- }
- }
- throw Error("Unknown reduction: " + reduction);
- };
- LossOps.absoluteDifference = function (labels, predictions, weights, reduction) {
- if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
- assertArgumentsAreTensors({ labels: labels, predictions: predictions }, 'absoluteDifference');
- if (weights != null) {
- assertArgumentsAreTensors({ weights: weights }, 'absoluteDifference');
- }
- assertShapesMatch(labels.shape, predictions.shape, 'Error in absoluteDifference: ');
- var losses$$1 = labels.sub(predictions).abs();
- return LossOps.computeWeightedLoss(losses$$1, weights, reduction);
- };
- LossOps.meanSquaredError = function (labels, predictions, weights, reduction) {
- if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
- assertArgumentsAreTensors({ labels: labels, predictions: predictions }, 'meanSquaredError');
- if (weights != null) {
- assertArgumentsAreTensors({ weights: weights }, 'meanSquaredError');
- }
- assertShapesMatch(labels.shape, predictions.shape, 'Error in meanSquaredError: ');
- var losses$$1 = labels.squaredDifference(predictions);
- return LossOps.computeWeightedLoss(losses$$1, weights, reduction);
- };
- LossOps.cosineDistance = function (labels, predictions, axis, weights, reduction) {
- if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
- assertArgumentsAreTensors({ labels: labels, predictions: predictions }, 'cosineDistance');
- if (weights != null) {
- assertArgumentsAreTensors({ weights: weights }, 'cosineDistance');
- }
- assertShapesMatch(labels.shape, predictions.shape, 'Error in cosineDistance: ');
- var one = scalar(1);
- var losses$$1 = one.sub(labels.mul(predictions).sum(axis, true));
- return LossOps.computeWeightedLoss(losses$$1, weights, reduction);
- };
- LossOps.hingeLoss = function (labels, predictions, weights, reduction) {
- if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
- assertArgumentsAreTensors({ labels: labels, predictions: predictions }, 'hingeLoss');
- if (weights != null) {
- assertArgumentsAreTensors({ weights: weights }, 'hingeLoss');
- }
- assertShapesMatch(labels.shape, predictions.shape, 'Error in hingeLoss: ');
- var one = scalar(1);
- labels = scalar(2).mul(labels).sub(one);
- var losses$$1 = one.sub(labels.mul(predictions)).relu();
- return LossOps.computeWeightedLoss(losses$$1, weights, reduction);
- };
- LossOps.logLoss = function (labels, predictions, weights, epsilon, reduction) {
- if (epsilon === void 0) { epsilon = 1e-7; }
- if (reduction === void 0) { reduction = Reduction.SUM_BY_NONZERO_WEIGHTS; }
- assertArgumentsAreTensors({ labels: labels, predictions: predictions }, 'logLoss');
- if (weights != null) {
- assertArgumentsAreTensors({ weights: weights }, 'logLoss');
- }
- assertShapesMatch(labels.shape, predictions.shape, 'Error in logLoss: ');
- var one = scalar(1);
- var epsilonScalar = scalar(epsilon);
- var losses$$1 = labels.mul(predictions.add(epsilonScalar).log())
- .neg()
- .sub(one.sub(labels).mul(one.sub(predictions).add(epsilonScalar).log()));
- return LossOps.computeWeightedLoss(losses$$1, weights, reduction);
- };
- __decorate$b([
- doc({ heading: 'Training', subheading: 'Losses', namespace: 'losses' }),
- operation
- ], LossOps, "computeWeightedLoss", null);
- __decorate$b([
- doc({ heading: 'Training', subheading: 'Losses', namespace: 'losses' }),
- operation
- ], LossOps, "absoluteDifference", null);
- __decorate$b([
- doc({ heading: 'Training', subheading: 'Losses', namespace: 'losses' }),
- operation
- ], LossOps, "meanSquaredError", null);
- __decorate$b([
- doc({ heading: 'Training', subheading: 'Losses', namespace: 'losses' }),
- operation
- ], LossOps, "cosineDistance", null);
- __decorate$b([
- doc({ heading: 'Training', subheading: 'Losses', namespace: 'losses' }),
- operation
- ], LossOps, "hingeLoss", null);
- __decorate$b([
- doc({ heading: 'Training', subheading: 'Losses', namespace: 'losses' }),
- operation
- ], LossOps, "logLoss", null);
- return LossOps;
- }());
-
- var __decorate$c = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var LRNOps = (function () {
- function LRNOps() {
- }
- LRNOps.localResponseNormalization = function (x, radius, bias, alpha, beta) {
- if (radius === void 0) { radius = 5; }
- if (bias === void 0) { bias = 1; }
- if (alpha === void 0) { alpha = 1; }
- if (beta === void 0) { beta = 0.5; }
- assertArgumentsAreTensors({ x: x }, 'localResponseNormalization');
- assert(x.rank === 4 || x.rank === 3, "Error in localResponseNormalization: x must be rank 3 or 4 but got\n rank " + x.rank + ".");
- assert(isInt(radius), "Error in localResponseNormalization3D: radius must be an integer\n but got radius " + radius + ".");
- var x4D = x;
- var reshapedTo4D = false;
- if (x.rank === 3) {
- reshapedTo4D = true;
- x4D = x.as4D(1, x.shape[0], x.shape[1], x.shape[2]);
- }
- var res = ENV.engine.runKernel(function (backend) { return backend.localResponseNormalization4D(x4D, radius, bias, alpha, beta); }, { x4D: x4D });
- if (reshapedTo4D) {
- return res.as3D(res.shape[1], res.shape[2], res.shape[3]);
- }
- else {
- return res;
- }
- };
- __decorate$c([
- doc({ heading: 'Operations', subheading: 'Normalization' }),
- operation
- ], LRNOps, "localResponseNormalization", null);
- return LRNOps;
- }());
-
- var __decorate$d = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var LSTMOps = (function () {
- function LSTMOps() {
- }
- LSTMOps.multiRNNCell = function (lstmCells, data, c, h) {
- assertArgumentsAreTensors({ data: data, c: c, h: h }, 'multiRNNCell');
- var input = data;
- var newStates = [];
- for (var i = 0; i < lstmCells.length; i++) {
- var output = lstmCells[i](input, c[i], h[i]);
- newStates.push(output[0]);
- newStates.push(output[1]);
- input = output[1];
- }
- var newC = [];
- var newH = [];
- for (var i = 0; i < newStates.length; i += 2) {
- newC.push(newStates[i]);
- newH.push(newStates[i + 1]);
- }
- return [newC, newH];
- };
- LSTMOps.basicLSTMCell = function (forgetBias, lstmKernel, lstmBias, data, c, h) {
- assertArgumentsAreTensors({ forgetBias: forgetBias, lstmKernel: lstmKernel, lstmBias: lstmBias, data: data, c: c, h: h }, 'basicLSTMCell');
- var combined = data.concat(h, 1);
- var weighted = combined.matMul(lstmKernel);
- var res = weighted.add(lstmBias);
- var batchSize = res.shape[0];
- var sliceCols = res.shape[1] / 4;
- var sliceSize = [batchSize, sliceCols];
- var i = res.slice([0, 0], sliceSize);
- var j = res.slice([0, sliceCols], sliceSize);
- var f = res.slice([0, sliceCols * 2], sliceSize);
- var o = res.slice([0, sliceCols * 3], sliceSize);
- var newC = i.sigmoid().mulStrict(j.tanh()).addStrict(c.mulStrict(forgetBias.add(f).sigmoid()));
- var newH = newC.tanh().mulStrict(o.sigmoid());
- return [newC, newH];
- };
- __decorate$d([
- doc({ heading: 'Operations', subheading: 'RNN' }),
- operation
- ], LSTMOps, "multiRNNCell", null);
- __decorate$d([
- doc({ heading: 'Operations', subheading: 'RNN' }),
- operation
- ], LSTMOps, "basicLSTMCell", null);
- return LSTMOps;
- }());
-
- var __decorate$e = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var MatmulOps = (function () {
- function MatmulOps() {
- }
- MatmulOps.matMul = function (a, b, transposeA, transposeB) {
- if (transposeA === void 0) { transposeA = false; }
- if (transposeB === void 0) { transposeB = false; }
- assertArgumentsAreTensors({ a: a, b: b }, 'matMul');
- var innerShapeA = transposeA ? a.shape[0] : a.shape[1];
- var innerShapeB = transposeB ? b.shape[1] : b.shape[0];
- assert(a.rank === 2 && b.rank === 2, "Error in matMul: inputs must be rank 2, got ranks " + a.rank +
- (" and " + b.rank + "."));
- assert(innerShapeA === innerShapeB, "Error in matMul: inner shapes (" + innerShapeA + ") and (" +
- (innerShapeB + ") of Tensors with shapes " + a.shape + " and ") +
- (b.shape + " and transposeA=" + transposeA) +
- (" and transposeB=" + transposeB + " must match."));
- var grad = function (dy) {
- if (!transposeA && !transposeB) {
- return {
- a: function () { return dy.matMul(b.toFloat(), false, true); },
- b: function () { return a.toFloat().matMul(dy, true, false); }
- };
- }
- else if (!transposeA && transposeB) {
- return {
- a: function () { return dy.matMul(b.toFloat(), false, false); },
- b: function () { return dy.matMul(a.toFloat(), true, false); }
- };
- }
- else if (transposeA && !transposeB) {
- return {
- a: function () { return b.toFloat().matMul(dy, false, true); },
- b: function () { return a.toFloat().matMul(dy, false, false); }
- };
- }
- else {
- return {
- a: function () { return b.toFloat().matMul(dy, true, true); },
- b: function () { return dy.matMul(a.toFloat(), true, true); }
- };
- }
- };
- return ENV.engine.runKernel(function (backend) { return backend.matMul(a, b, transposeA, transposeB); }, { a: a, b: b }, grad);
- };
- MatmulOps.vectorTimesMatrix = function (v, matrix) {
- assert(v.rank === 1, "Error in vectorTimesMatrix: first input must be rank 1, but got " +
- ("rank " + v.rank + "."));
- assert(matrix.rank === 2, "Error in vectorTimesMatrix: second input must be rank 2, but got " +
- ("rank " + matrix.rank + "."));
- assert(v.size === matrix.shape[0], "Error in vectorTimesMatrix: size of vector (" + v.size + ") " +
- ("must match first dimension of matrix (" + matrix.shape[0] + ")"));
- return v.as2D(1, -1).matMul(matrix).as1D();
- };
- MatmulOps.matrixTimesVector = function (matrix, v) {
- assert(v.rank === 1, "Error in matrixTimesVector: second input must rank 1, but got " +
- ("rank " + v.rank + "."));
- assert(matrix.rank === 2, "Error in matrixTimesVector: first input must be a rank 2, but got " +
- ("rank " + matrix.rank + "."));
- assert(v.size === matrix.shape[1], "Error in matrixTimesVector: size of first rank 1 input " + v.size + " " +
- "must match inner dimension of second rank 2 input, but got " +
- ("shape " + matrix.shape + "."));
- return matrix.matMul(v.as2D(-1, 1)).as1D();
- };
- MatmulOps.dotProduct = function (v1, v2) {
- assert(v1.rank === 1 && v2.rank === 1, "Error in dotProduct: inputs must be rank 1, but got ranks " +
- (v1.rank + " and " + v2.rank + "."));
- assert(v1.size === v2.size, "Error in dotProduct: size of inputs (" + v1.size + ") and (" +
- (v2.size + ") must match."));
- return v1.as2D(1, -1).matMul(v2.as2D(-1, 1)).asScalar();
- };
- MatmulOps.outerProduct = function (v1, v2) {
- assert(v1.rank === 1 && v2.rank === 1, "Error in outerProduct: inputs must be rank 1, but got ranks " +
- (v1.rank + " and " + v2.rank + "."));
- return v1.as2D(-1, 1).matMul(v2.as2D(1, -1));
- };
- __decorate$e([
- doc({ heading: 'Operations', subheading: 'Matrices' }),
- operation
- ], MatmulOps, "matMul", null);
- __decorate$e([
- operation
- ], MatmulOps, "vectorTimesMatrix", null);
- __decorate$e([
- operation
- ], MatmulOps, "matrixTimesVector", null);
- __decorate$e([
- operation
- ], MatmulOps, "dotProduct", null);
- __decorate$e([
- doc({ heading: 'Operations', subheading: 'Matrices' }),
- operation
- ], MatmulOps, "outerProduct", null);
- return MatmulOps;
- }());
-
- var __decorate$f = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var MovingAverageOps = (function () {
- function MovingAverageOps() {
- }
- MovingAverageOps.movingAverage = function (v, x, decay, step, zeroDebias) {
- if (zeroDebias === void 0) { zeroDebias = true; }
- assertArgumentsAreTensors({ v: v, x: x }, 'movingAverage');
- assertTypesMatch(v, x);
- assert(arraysEqual(v.shape, x.shape), 'Shape mismatch in v and x');
- var one = ArrayOps.scalar(1);
- decay = typeof decay === 'number' ? ArrayOps.scalar(decay) : decay;
- var oneMinusDecay = one.sub(decay);
- var update = x.sub(v).mul(oneMinusDecay);
- if (zeroDebias) {
- assert(step != null, 'When using zeroDebias: true, step is required.');
- step = typeof step === 'number' ? ArrayOps.scalar(step) : step;
- update = update.div(one.sub(BinaryOps.pow(decay, step)));
- }
- return v.add(update);
- };
- __decorate$f([
- doc({ heading: 'Operations', subheading: 'Moving Average' }),
- operation
- ], MovingAverageOps, "movingAverage", null);
- return MovingAverageOps;
- }());
-
- var __decorate$g = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var NormOps = (function () {
- function NormOps() {
- }
- NormOps.norm = function (x, ord, axis, keepDims) {
- if (ord === void 0) { ord = 'euclidean'; }
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- assertArgumentsAreTensors({ x: x }, 'norm');
- var norm$$1 = normImpl(x, ord, axis);
- var keepDimsShape = norm$$1.shape;
- if (keepDims) {
- var axes = parseAxisParam(axis, x.shape);
- keepDimsShape = expandShapeToKeepDim(norm$$1.shape, axes);
- }
- return norm$$1.reshape(keepDimsShape);
- };
- __decorate$g([
- doc({ heading: 'Operations', subheading: 'Matrices' }),
- operation
- ], NormOps, "norm", null);
- return NormOps;
- }());
- function normImpl(x, p, axis) {
- if (axis === void 0) { axis = null; }
- if (x.rank === 0) {
- return x.abs();
- }
- if (x.rank !== 1 && axis === null) {
- return normImpl(x.reshape([-1]), p, axis);
- }
- if (x.rank === 1 || typeof axis === 'number' ||
- axis instanceof Array && axis.length === 1) {
- if (p === 1) {
- return x.abs().sum(axis);
- }
- if (p === Infinity) {
- return x.abs().max(axis);
- }
- if (p === -Infinity) {
- return x.abs().min(axis);
- }
- if (p === 'euclidean' || p === 2) {
- return x.abs().pow(scalar(2, 'int32')).sum(axis).sqrt();
- }
- throw new Error("Error in norm: invalid ord value: " + p);
- }
- if (axis instanceof Array && axis.length === 2) {
- if (p === 1) {
- return x.abs().sum(axis[0]).max(axis[1] - 1);
- }
- if (p === Infinity) {
- return x.abs().sum(axis[1]).max(axis[0]);
- }
- if (p === -Infinity) {
- return x.abs().sum(axis[1]).min(axis[0]);
- }
- if (p === 'fro' || p === 'euclidean') {
- return x.square().sum(axis).sqrt();
- }
- throw new Error("Error in norm: invalid ord value: " + p);
- }
- throw new Error("Error in norm: invalid axis: " + axis);
- }
-
- var __decorate$h = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var PoolOps = (function () {
- function PoolOps() {
- }
- PoolOps.maxPool = function (x, filterSize, strides, pad, dimRoundingMode) {
- assertArgumentsAreTensors({ x: x }, 'maxPool');
- var x4D = x;
- var reshapedTo4D = false;
- if (x.rank === 3) {
- reshapedTo4D = true;
- x4D = x.as4D(1, x.shape[0], x.shape[1], x.shape[2]);
- }
- assert(x4D.rank === 4, "Error in maxPool: input must be rank 4 but got rank " + x4D.rank + ".");
- if (dimRoundingMode != null) {
- assert(isInt(pad), "Error in maxPool: pad must be an integer when using, " +
- ("dimRoundingMode " + dimRoundingMode + " but got pad " + pad + "."));
- }
- var convInfo = computePool2DInfo(x4D.shape, filterSize, strides, pad, dimRoundingMode);
- var grad = function (dy, saved) {
- var y4D = saved[0];
- return {
- x: function () { return PoolOps.maxPoolBackprop(dy, x4D, y4D, filterSize, strides, pad); }
- };
- };
- var res = ENV.engine.runKernel(function (backend, save) { return save(backend.maxPool(x4D, convInfo)); }, { x: x4D }, grad);
- if (reshapedTo4D) {
- return res.as3D(res.shape[1], res.shape[2], res.shape[3]);
- }
- return res;
- };
- PoolOps.maxPoolBackprop = function (dy, input, output, filterSize, strides, pad, dimRoundingMode) {
- assertArgumentsAreTensors({ dy: dy, input: input, output: output }, 'maxPoolBackprop');
- assert(input.rank === dy.rank, "Rank of input (" + input.rank + ") does not match rank of dy (" + dy.rank + ")");
- assert(dy.rank === 4, "Error in maxPoolBackprop: dy must be rank 4 but got rank " +
- (dy.rank + "."));
- assert(input.rank === 4, "Error in maxPoolBackprop: input must be rank 4 but got rank " +
- (input.rank + "."));
- if (dimRoundingMode != null) {
- assert(isInt(pad), "Error in maxPoolBackprop: pad must be an integer when using, " +
- ("dimRoundingMode " + dimRoundingMode + " but got pad " + pad + "."));
- }
- var convInfo = computePool2DInfo(input.shape, filterSize, strides, pad, dimRoundingMode);
- var res = ENV.engine.runKernel(function (backend) { return backend.maxPoolBackprop(dy, input, output, convInfo); }, { dy: dy, input: input });
- return res;
- };
- PoolOps.avgPool = function (x, filterSize, strides, pad, dimRoundingMode) {
- assertArgumentsAreTensors({ x: x }, 'avgPool');
- assert(x.dtype === 'float32', 'The input dtype to avgPool must be float32');
- var x4D = x;
- var reshapedTo4D = false;
- if (x.rank === 3) {
- reshapedTo4D = true;
- x4D = x.as4D(1, x.shape[0], x.shape[1], x.shape[2]);
- }
- assert(x4D.rank === 4, "Error in avgPool: x must be rank 4 but got rank " + x4D.rank + ".");
- if (dimRoundingMode != null) {
- assert(isInt(pad), "Error in avgPool: pad must be an integer when using, " +
- ("dimRoundingMode " + dimRoundingMode + " but got pad " + pad + "."));
- }
- var convInfo = computePool2DInfo(x4D.shape, filterSize, strides, pad);
- var grad = function (dy) {
- return {
- x: function () { return PoolOps.avgPoolBackprop(dy, x4D, filterSize, strides, pad); }
- };
- };
- var res = ENV.engine.runKernel(function (backend) { return backend.avgPool(x4D, convInfo); }, { x: x4D }, grad);
- res = res.cast(x.dtype);
- if (reshapedTo4D) {
- return res.as3D(res.shape[1], res.shape[2], res.shape[3]);
- }
- return res;
- };
- PoolOps.avgPoolBackprop = function (dy, input, filterSize, strides, pad) {
- assertArgumentsAreTensors({ dy: dy, input: input }, 'avgPoolBackprop');
- assert(input.rank === dy.rank, "Rank of input (" + input.rank + ") does not match rank of dy (" + dy.rank + ")");
- var input4D = input;
- var dy4D = dy;
- var reshapedTo4D = false;
- if (input.rank === 3) {
- reshapedTo4D = true;
- input4D = input.as4D(1, input.shape[0], input.shape[1], input.shape[2]);
- dy4D = dy.as4D(1, dy.shape[0], dy.shape[1], dy.shape[2]);
- }
- assert(dy4D.rank === 4, "Error in avgPoolBackprop: dy must be rank 4 but got rank " +
- (dy4D.rank + "."));
- assert(input4D.rank === 4, "Error in avgPoolBackprop: input must be rank 4 but got rank " +
- (input4D.rank + "."));
- var convInfo = computePool2DInfo(input4D.shape, filterSize, strides, pad);
- var res = ENV.engine.runKernel(function (backend) { return backend.avgPoolBackprop(dy4D, input4D, convInfo); }, { dy4D: dy4D, input4D: input4D });
- if (reshapedTo4D) {
- return res.as3D(res.shape[1], res.shape[2], res.shape[3]);
- }
- return res;
- };
- __decorate$h([
- doc({ heading: 'Operations', subheading: 'Convolution' }),
- operation
- ], PoolOps, "maxPool", null);
- __decorate$h([
- operation
- ], PoolOps, "maxPoolBackprop", null);
- __decorate$h([
- doc({ heading: 'Operations', subheading: 'Convolution' }),
- operation
- ], PoolOps, "avgPool", null);
- __decorate$h([
- operation
- ], PoolOps, "avgPoolBackprop", null);
- return PoolOps;
- }());
-
- var __decorate$i = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var ReverseOps = (function () {
- function ReverseOps() {
- }
- ReverseOps.reverse1d = function (x) {
- assert(x.rank === 1, "Error in reverse1D: x must be rank 1 but got\n rank " + x.rank + ".");
- return ReverseOps.reverse(x, 0);
- };
- ReverseOps.reverse2d = function (x, axis) {
- assert(x.rank === 2, "Error in reverse2D: x must be rank 2 but got\n rank " + x.rank + ".");
- return ReverseOps.reverse(x, axis);
- };
- ReverseOps.reverse3d = function (x, axis) {
- assert(x.rank === 3, "Error in reverse3D: x must be rank 3 but got\n rank " + x.rank + ".");
- return ReverseOps.reverse(x, axis);
- };
- ReverseOps.reverse4d = function (x, axis) {
- assert(x.rank === 4, "Error in reverse4D: x must be rank 4 but got\n rank " + x.rank + ".");
- return ReverseOps.reverse(x, axis);
- };
- ReverseOps.reverse = function (x, axis) {
- assertArgumentsAreTensors({ x: x }, 'reverse');
- if (x.rank === 0) {
- return x.clone();
- }
- var axes = parseAxisParam(axis, x.shape);
- var grad = function (dy) {
- return { x: function () { return dy.reverse(axes); } };
- };
- var res = ENV.engine.runKernel(function (backend) { return backend.reverse(x, axes); }, { x: x }, grad);
- return res.reshapeAs(x);
- };
- __decorate$i([
- doc({ heading: 'Tensors', subheading: 'Slicing and Joining' }),
- operation
- ], ReverseOps, "reverse", null);
- return ReverseOps;
- }());
-
- function assertParamsValid(input, begin, size) {
- assert(input.rank === begin.length, "Error in slice" + input.rank + "D: Length of begin " + begin + " must " +
- ("match the rank of the array (" + input.rank + ")."));
- assert(input.rank === size.length, "Error in slice" + input.rank + "D: Length of size " + size + " must " +
- ("match the rank of the array (" + input.rank + ")."));
- for (var i = 0; i < input.rank; ++i) {
- assert(begin[i] + size[i] <= input.shape[i], "Error in slice" + input.rank + "D: begin[" + i + "] + size[" + i + "] " +
- ("(" + (begin[i] + size[i]) + ") would overflow input.shape[" + i + "] (" + input.shape[i] + ")"));
- }
- }
- function getStridedSlicedInfo(shape, begin, end, strides, beginMask, endMask) {
- if (beginMask === void 0) { beginMask = 0; }
- if (endMask === void 0) { endMask = 0; }
- var startIndex = [];
- var endIndex = [];
- for (var i = 0; i < shape.length; i++) {
- startIndex[i] = startForAxis(beginMask, begin, strides, shape, i);
- endIndex[i] = stopForAxis(endMask, end, strides, shape, i);
- }
- var size = new Array(shape.length).fill(0);
- size = size.map(function (d, i) {
- var count = 0;
- for (var start = startIndex[i]; !(strides[i] > 0 ? start >= endIndex[i] : start <= endIndex[i]); start += strides[i]) {
- count += 1;
- }
- return count;
- });
- return [startIndex, size];
- }
- function startForAxis(beginMask, startIndices, strides, inputShape, axis) {
- var start = startIndices[axis];
- if (beginMask & 1 << axis) {
- if (strides[axis] > 0) {
- start = Number.MIN_SAFE_INTEGER;
- }
- else {
- start = Number.MAX_SAFE_INTEGER;
- }
- }
- var axisSize = inputShape[axis];
- if (start < 0) {
- start += axisSize;
- }
- start = clamp(0, start, axisSize - 1);
- return start;
- }
- function stopForAxis(endMask, stopIndices, strides, inputShape, axis) {
- var stop = stopIndices[axis];
- if (endMask & (1 << axis)) {
- if (strides[axis] > 0) {
- stop = Number.MAX_SAFE_INTEGER;
- }
- else {
- stop = Number.MIN_SAFE_INTEGER;
- }
- }
- var axisSize = inputShape[axis];
- if (stop < 0) {
- stop += axisSize;
- }
- if (strides[axis] > 0) {
- stop = clamp(0, stop, axisSize);
- }
- else {
- stop = clamp(-1, stop, axisSize - 1);
- }
- return stop;
- }
-
- var __decorate$j = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var SliceOps = (function () {
- function SliceOps() {
- }
- SliceOps.slice1d = function (x, begin, size) {
- assert(x.rank === 1, "slice1d expects a rank-1 tensor, but got a rank-" + x.rank + " tensor");
- return SliceOps.slice(x, [begin], [size]);
- };
- SliceOps.slice2d = function (x, begin, size) {
- assert(x.rank === 2, "slice1d expects a rank-2 tensor, but got a rank-" + x.rank + " tensor");
- return SliceOps.slice(x, begin, size);
- };
- SliceOps.slice3d = function (x, begin, size) {
- assert(x.rank === 3, "slice1d expects a rank-3 tensor, but got a rank-" + x.rank + " tensor");
- return SliceOps.slice(x, begin, size);
- };
- SliceOps.slice4d = function (x, begin, size) {
- assert(x.rank === 4, "slice1d expects a rank-4 tensor, but got a rank-" + x.rank + " tensor");
- return SliceOps.slice(x, begin, size);
- };
- SliceOps.slice = function (x, begin, size) {
- assertArgumentsAreTensors({ x: x }, 'slice');
- if (x.rank === 0) {
- throw new Error('Slicing scalar is not possible');
- }
- var begin_;
- if (typeof begin === 'number') {
- begin_ = [begin].concat(new Array(x.rank - 1).fill(0));
- }
- else if (begin.length < x.rank) {
- begin_ = begin.concat(new Array(x.rank - begin.length).fill(0));
- }
- else {
- begin_ = begin;
- }
- var size_;
- if (size == null) {
- size_ = new Array(x.rank).fill(-1);
- }
- else if (typeof size === 'number') {
- size_ = [size].concat(new Array(x.rank - 1).fill(-1));
- }
- else if (size.length < x.rank) {
- size_ = size.concat(new Array(x.rank - size.length).fill(-1));
- }
- else {
- size_ = size;
- }
- size_ = size_.map(function (d, i) {
- if (d >= 0) {
- return d;
- }
- else {
- assert(d === -1, 'Bad value in size');
- return x.shape[i] - begin_[i];
- }
- });
- assertParamsValid(x, begin_, size_);
- var inputShape = x.shape;
- var grad = function (dy) {
- var paddings = [];
- for (var i = 0; i < dy.rank; i++) {
- paddings.push([begin_[i], inputShape[i] - begin_[i] - size_[i]]);
- }
- return { x: function () { return dy.pad(paddings); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.slice(x, begin_, size_); }, { x: x }, grad);
- };
- __decorate$j([
- doc({ heading: 'Tensors', subheading: 'Slicing and Joining' }),
- operation
- ], SliceOps, "slice", null);
- return SliceOps;
- }());
-
- var __decorate$k = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var SoftmaxOps = (function () {
- function SoftmaxOps() {
- }
- SoftmaxOps.softmax = function (logits, dim) {
- if (dim === void 0) { dim = -1; }
- assertArgumentsAreTensors({ logits: logits }, 'softmax');
- if (dim === -1) {
- dim = logits.rank - 1;
- }
- if (dim !== logits.rank - 1) {
- throw Error('Softmax along a non-last dimension is not yet supported. ' +
- ("Logits was rank " + logits.rank + " and dim was " + dim));
- }
- var customOp = customGrad(function (logits) {
- var keepDims = true;
- var lse = logits.logSumExp([dim], keepDims);
- var logResult = logits.toFloat().sub(lse);
- var y = logResult.exp();
- var gradFunc = function (dy) {
- var dyTimesY = dy.mul(y);
- var keepDims = true;
- return dyTimesY.sub(dyTimesY.sum([dim], keepDims).mul(y));
- };
- return { value: y, gradFunc: gradFunc };
- });
- return customOp(logits);
- };
- SoftmaxOps.softmaxCrossEntropy = function (labels, logits, dim) {
- if (dim === void 0) { dim = -1; }
- assertArgumentsAreTensors({ labels: labels, logits: logits }, 'softmaxCrossEntropy');
- assertShapesMatch(labels.shape, logits.shape, 'Error in softmaxCrossEntropy: ');
- if (dim === -1) {
- dim = logits.rank - 1;
- }
- if (dim !== logits.rank - 1) {
- throw Error("Softmax cross entropy along a non-last dimension is not yet " +
- ("supported. Labels / logits was rank " + logits.rank + " ") +
- ("and dim was " + dim));
- }
- var customOp = customGrad(function (labels, logits) {
- var predictedProbs = logits.softmax(dim);
- var costVector = scalar(1e-5).add(predictedProbs).log().mul(labels).neg();
- var value = costVector.sum([dim]);
- var gradFunc = function (dy) {
- var dyShape = expandShapeToKeepDim(dy.shape, [dim]);
- return [
- dy.reshape(dyShape).mul(labels.toFloat().sub(predictedProbs)),
- dy.reshape(dyShape).mul(predictedProbs.sub(labels.toFloat())),
- ];
- };
- return { value: value, gradFunc: gradFunc };
- });
- return customOp(labels, logits);
- };
- __decorate$k([
- doc({ heading: 'Operations', subheading: 'Normalization' }),
- operation
- ], SoftmaxOps, "softmax", null);
- __decorate$k([
- doc({ heading: 'Training', subheading: 'Losses', namespace: 'losses' }),
- operation
- ], SoftmaxOps, "softmaxCrossEntropy", null);
- return SoftmaxOps;
- }());
-
- var __decorate$l = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var StridedSliceOps = (function () {
- function StridedSliceOps() {
- }
- StridedSliceOps.stridedSlice = function (x, begin, end, strides, beginMask, endMask) {
- if (beginMask === void 0) { beginMask = 0; }
- if (endMask === void 0) { endMask = 0; }
- assertArgumentsAreTensors({ x: x }, 'stridedSlice');
- return ENV.engine.runKernel(function (backend) { return backend.stridedSlice(x, begin, end, strides, beginMask, endMask); }, { x: x });
- };
- __decorate$l([
- doc({ heading: 'Operations', subheading: 'Slicing and Joining' }),
- operation
- ], StridedSliceOps, "stridedSlice", null);
- return StridedSliceOps;
- }());
-
- var __decorate$m = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var TransposeOps = (function () {
- function TransposeOps() {
- }
- TransposeOps.transpose = function (x, perm) {
- assertArgumentsAreTensors({ x: x }, 'transpose');
- if (perm == null) {
- perm = x.shape.map(function (s, i) { return i; }).reverse();
- }
- assert(x.rank === perm.length, "Error in transpose: rank of input " + x.rank + " " +
- ("must match length of perm " + perm + "."));
- perm.forEach(function (axis) {
- assert(axis >= 0 && axis < x.rank, "All entries in 'perm' must be between 0 and " + (x.rank - 1) +
- (" but got " + perm));
- });
- if (x.rank <= 1) {
- return x.clone();
- }
- var der = function (dy) {
- var undoPerm = getUndoAxesPermutation(perm);
- return { x: function () { return dy.transpose(undoPerm); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.transpose(x, perm); }, { x: x }, der);
- };
- __decorate$m([
- doc({ heading: 'Operations', subheading: 'Matrices' }),
- operation
- ], TransposeOps, "transpose", null);
- return TransposeOps;
- }());
-
- var SELU_SCALEALPHA = 1.7580993408473768599402175208123;
- var SELU_SCALE = 1.0507009873554804934193349852946;
-
- var __decorate$n = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var UnaryOps = (function () {
- function UnaryOps() {
- }
- UnaryOps.neg = function (x) {
- assertArgumentsAreTensors({ x: x }, 'neg');
- var grad = function (dy) {
- return { x: function () { return dy.neg(); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.neg(x); }, { x: x }, grad);
- };
- UnaryOps.ceil = function (x) {
- assertArgumentsAreTensors({ x: x }, 'ceil');
- var grad = function (dy) {
- return { x: function () { return zerosLike(dy); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.ceil(x); }, { x: x }, grad);
- };
- UnaryOps.floor = function (x) {
- assertArgumentsAreTensors({ x: x }, 'floor');
- var grad = function (dy) {
- return { x: function () { return zerosLike(dy); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.floor(x); }, { x: x }, grad);
- };
- UnaryOps.sign = function (x) {
- assertArgumentsAreTensors({ x: x }, 'sign');
- var grad = function (dy) {
- return { x: function () { return zerosLike(dy); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.sign(x); }, { x: x }, grad);
- };
- UnaryOps.round = function (x) {
- assertArgumentsAreTensors({ x: x }, 'round');
- var grad = function (dy) {
- return { x: function () { return zerosLike(dy); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.round(x); }, { x: x }, grad);
- };
- UnaryOps.exp = function (x) {
- assertArgumentsAreTensors({ x: x }, 'exp');
- var bck = function (dy, saved) {
- var y = saved[0];
- return { x: function () { return dy.mulStrict(y); } };
- };
- return ENV.engine.runKernel(function (backend, save) { return save(backend.exp(x)); }, { x: x }, bck);
- };
- UnaryOps.expm1 = function (x) {
- assertArgumentsAreTensors({ x: x }, 'expm1');
- var grad = function (dy) {
- return { x: function () { return dy.mulStrict(x.exp()); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.expm1(x); }, { x: x }, grad);
- };
- UnaryOps.log = function (x) {
- assertArgumentsAreTensors({ x: x }, 'log');
- var grad = function (dy) {
- return { x: function () { return dy.divStrict(x.toFloat()); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.log(x); }, { x: x }, grad);
- };
- UnaryOps.log1p = function (x) {
- assertArgumentsAreTensors({ x: x }, 'log1p');
- var grad = function (dy) {
- return { x: function () { return dy.divStrict(x.add(scalar(1))); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.log1p(x); }, { x: x }, grad);
- };
- UnaryOps.sqrt = function (x) {
- assertArgumentsAreTensors({ x: x }, 'sqrt');
- var grad = function (dy) {
- return { x: function () { return dy.divStrict(x.toFloat().sqrt().mul(scalar(2))); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.sqrt(x); }, { x: x }, grad);
- };
- UnaryOps.rsqrt = function (x) {
- assertArgumentsAreTensors({ x: x }, 'rsqrt');
- var grad = function (dy) {
- return {
- x: function () { return dy.divStrict(x.pow(scalar(1.5)).mul(scalar(2))).neg(); }
- };
- };
- return ENV.engine.runKernel(function (backend) { return backend.rsqrt(x); }, { x: x }, grad);
- };
- UnaryOps.square = function (x) {
- assertArgumentsAreTensors({ x: x }, 'square');
- var grad = function (dy) {
- return { x: function () { return dy.mulStrict(x.toFloat().mul(scalar(2))); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.square(x); }, { x: x }, grad);
- };
- UnaryOps.reciprocal = function (x) {
- assertArgumentsAreTensors({ x: x }, 'reciprocal');
- var grad = function (dy) {
- return { x: function () { return dy.divStrict(x.square().neg()); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.reciprocal(x); }, { x: x }, grad);
- };
- UnaryOps.abs = function (x) {
- assertArgumentsAreTensors({ x: x }, 'abs');
- var grad = function (dy) {
- return { x: function () { return dy.mulStrict(x.toFloat().step(-1)); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.abs(x); }, { x: x }, grad);
- };
- UnaryOps.clipByValue = function (x, clipValueMin, clipValueMax) {
- assertArgumentsAreTensors({ x: x }, 'clipByValue');
- assert((clipValueMin <= clipValueMax), "Error in clip: min (" + clipValueMin + ") must be " +
- ("less than or equal to max (" + clipValueMax + ")."));
- var grad = function (dy) {
- return {
- x: function () { return dy.where(x.greater(scalar(clipValueMin))
- .logicalAnd(x.less(scalar(clipValueMax))), zerosLike(dy)); },
- };
- };
- return ENV.engine.runKernel(function (backend) { return backend.clip(x, clipValueMin, clipValueMax); }, { x: x }, grad);
- };
- UnaryOps.relu = function (x) {
- assertArgumentsAreTensors({ x: x }, 'relu');
- if (x.dtype === 'bool') {
- return x.toInt();
- }
- var grad = function (dy) {
- var stepRes = x.step();
- return { x: function () { return dy.mulStrict(stepRes.toFloat()); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.relu(x); }, { x: x }, grad);
- };
- UnaryOps.elu = function (x) {
- assertArgumentsAreTensors({ x: x }, 'elu');
- var grad = function (dy, saved) {
- var y = saved[0];
- return {
- x: function () {
- return ENV.engine.runKernel(function (backend) { return backend.eluDer(dy, y); }, { dy: dy, y: y });
- }
- };
- };
- return ENV.engine.runKernel(function (backend, save) { return save(backend.elu(x)); }, { x: x }, grad);
- };
- UnaryOps.selu = function (x) {
- assertArgumentsAreTensors({ x: x }, 'selu');
- var grad = function (dy) {
- return {
- x: function () {
- var mask = x.greater(scalar(0));
- var scaleAlpha = scalar(SELU_SCALEALPHA);
- var scale = scalar(SELU_SCALE);
- var greaterThanZeroDer = dy.mul(scale);
- var lessEqualZeroDer = dy.mul(scaleAlpha).mul(x.toFloat().exp());
- return where(mask, greaterThanZeroDer, lessEqualZeroDer);
- }
- };
- };
- return ENV.engine.runKernel(function (backend) { return backend.selu(x); }, { x: x }, grad);
- };
- UnaryOps.leakyRelu = function (x, alpha) {
- if (alpha === void 0) { alpha = 0.2; }
- assertArgumentsAreTensors({ x: x }, 'leakyRelu');
- return maximum(scalar(alpha).mul(x), x);
- };
- UnaryOps.prelu = function (x, alpha) {
- assertArgumentsAreTensors({ x: x, alpha: alpha }, 'prelu');
- var zero = scalar(0);
- return maximum(zero, x).add(alpha.mul(minimum(zero, x)));
- };
- UnaryOps.sigmoid = function (x) {
- assertArgumentsAreTensors({ x: x }, 'sigmoid');
- var grad = function (dy, saved) {
- var y = saved[0];
- return { x: function () { return dy.mulStrict(y.mul(scalar(1).sub(y))); } };
- };
- return ENV.engine.runKernel(function (backend, save) { return save(backend.sigmoid(x)); }, { x: x }, grad);
- };
- UnaryOps.logSigmoid = function (x) {
- assertArgumentsAreTensors({ x: x }, 'logSigmoid');
- var grad = function (dy) {
- return { x: function () { return dy.mulStrict(x.neg().sigmoid()); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.softplus(x.neg()).neg(); }, { x: x }, grad);
- };
- UnaryOps.softplus = function (x) {
- assertArgumentsAreTensors({ x: x }, 'softplus');
- var grad = function (dy) {
- return { x: function () { return dy.mulStrict(x.sigmoid()); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.softplus(x); }, { x: x }, grad);
- };
- UnaryOps.sin = function (x) {
- assertArgumentsAreTensors({ x: x }, 'sin');
- var grad = function (dy) {
- return { x: function () { return x.toFloat().cos().mulStrict(dy); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.sin(x); }, { x: x }, grad);
- };
- UnaryOps.cos = function (x) {
- assertArgumentsAreTensors({ x: x }, 'cos');
- var grad = function (dy) {
- return { x: function () { return x.toFloat().sin().neg().mulStrict(dy); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.cos(x); }, { x: x }, grad);
- };
- UnaryOps.tan = function (x) {
- assertArgumentsAreTensors({ x: x }, 'tan');
- var grad = function (dy) {
- return { x: function () { return dy.divStrict(x.cos().square()); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.tan(x); }, { x: x }, grad);
- };
- UnaryOps.asin = function (x) {
- assertArgumentsAreTensors({ x: x }, 'asin');
- var grad = function (dy) {
- return {
- x: function () {
- return dy.divStrict(UnaryOps.sqrt(scalar(1).sub(x.toFloat().square())));
- }
- };
- };
- return ENV.engine.runKernel(function (backend) { return backend.asin(x); }, { x: x }, grad);
- };
- UnaryOps.acos = function (x) {
- assertArgumentsAreTensors({ x: x }, 'acos');
- var grad = function (dy) {
- return {
- x: function () {
- return dy.divStrict(UnaryOps.sqrt(scalar(1).sub(x.toFloat().square())))
- .neg();
- }
- };
- };
- return ENV.engine.runKernel(function (backend) { return backend.acos(x); }, { x: x }, grad);
- };
- UnaryOps.atan = function (x) {
- assertArgumentsAreTensors({ x: x }, 'atan');
- var grad = function (dy) {
- return { x: function () { return dy.divStrict(scalar(1).add(x.toFloat().square())); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.atan(x); }, { x: x }, grad);
- };
- UnaryOps.sinh = function (x) {
- assertArgumentsAreTensors({ x: x }, 'sinh');
- var grad = function (dy) {
- return { x: function () { return x.toFloat().cosh().mulStrict(dy); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.sinh(x); }, { x: x }, grad);
- };
- UnaryOps.cosh = function (x) {
- assertArgumentsAreTensors({ x: x }, 'cosh');
- var grad = function (dy) {
- return { x: function () { return x.toFloat().sinh().mulStrict(dy); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.cosh(x); }, { x: x }, grad);
- };
- UnaryOps.tanh = function (x) {
- assertArgumentsAreTensors({ x: x }, 'tanh');
- var grad = function (dy, saved) {
- var y = saved[0];
- return { x: function () { return scalar(1).sub(y.square()).mulStrict(dy); } };
- };
- return ENV.engine.runKernel(function (backend, save) { return save(backend.tanh(x)); }, { x: x }, grad);
- };
- UnaryOps.asinh = function (x) {
- assertArgumentsAreTensors({ x: x }, 'asinh');
- var grad = function (dy) {
- return {
- x: function () {
- return dy.divStrict(UnaryOps.sqrt(scalar(1).add(x.toFloat().square())));
- }
- };
- };
- return ENV.engine.runKernel(function (backend) { return backend.asinh(x); }, { x: x }, grad);
- };
- UnaryOps.acosh = function (x) {
- assertArgumentsAreTensors({ x: x }, 'acosh');
- var grad = function (dy) {
- return {
- x: function () {
- return dy.divStrict(UnaryOps.sqrt(x.toFloat().square().sub(scalar(1))));
- }
- };
- };
- return ENV.engine.runKernel(function (backend) { return backend.acosh(x); }, { x: x }, grad);
- };
- UnaryOps.atanh = function (x) {
- assertArgumentsAreTensors({ x: x }, 'atanh');
- var grad = function (dy) {
- return { x: function () { return dy.divStrict(scalar(1).sub(x.toFloat().square())); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.atanh(x); }, { x: x }, grad);
- };
- UnaryOps.erf = function (x) {
- assert(x.dtype === 'int32' || x.dtype === 'float32', 'Input dtype must be `int32` or `float32`.');
- if (x.dtype === 'int32') {
- x = x.toFloat();
- }
- var grad = function (dy) {
- return {
- x: function () {
- return dy.mulStrict(scalar(2 / Math.sqrt(Math.PI))
- .mul(x.square().neg().exp()));
- }
- };
- };
- return ENV.engine.runKernel(function (backend) { return backend.erf(x); }, { x: x }, grad);
- };
- UnaryOps.step = function (x, alpha) {
- if (alpha === void 0) { alpha = 0.0; }
- assertArgumentsAreTensors({ x: x }, 'step');
- var grad = function (dy) {
- return { x: function () { return zerosLike(dy); } };
- };
- return ENV.engine.runKernel(function (backend) { return backend.step(x, alpha); }, { x: x }, grad);
- };
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "neg", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "ceil", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "floor", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "sign", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "round", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "exp", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "expm1", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "log", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "log1p", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "sqrt", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "rsqrt", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "square", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "reciprocal", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "abs", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "clipByValue", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "relu", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "elu", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "selu", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "leakyRelu", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "prelu", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "sigmoid", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "logSigmoid", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "softplus", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "sin", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "cos", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "tan", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "asin", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "acos", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "atan", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "sinh", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "cosh", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "tanh", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "asinh", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "acosh", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "atanh", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "erf", null);
- __decorate$n([
- doc({ heading: 'Operations', subheading: 'Basic math' }),
- operation
- ], UnaryOps, "step", null);
- return UnaryOps;
- }());
-
- var batchNormalization = BatchNormOps.batchNormalization;
- var batchNormalization2d = BatchNormOps.batchNormalization2d;
- var batchNormalization3d = BatchNormOps.batchNormalization3d;
- var batchNormalization4d = BatchNormOps.batchNormalization4d;
- var concat = ConcatOps.concat;
- var concat1d = ConcatOps.concat1d;
- var concat2d = ConcatOps.concat2d;
- var concat3d = ConcatOps.concat3d;
- var concat4d = ConcatOps.concat4d;
- var conv1d = ConvOps.conv1d;
- var conv2d = ConvOps.conv2d;
- var conv2dTranspose = ConvOps.conv2dTranspose;
- var depthwiseConv2d = ConvOps.depthwiseConv2d;
- var separableConv2d = ConvOps.separableConv2d;
- var matMul = MatmulOps.matMul;
- var matrixTimesVector = MatmulOps.matrixTimesVector;
- var outerProduct = MatmulOps.outerProduct;
- var vectorTimesMatrix = MatmulOps.vectorTimesMatrix;
- var avgPool = PoolOps.avgPool;
- var maxPool = PoolOps.maxPool;
- var transpose = TransposeOps.transpose;
- var reverse = ReverseOps.reverse;
- var reverse1d = ReverseOps.reverse1d;
- var reverse2d = ReverseOps.reverse2d;
- var reverse3d = ReverseOps.reverse3d;
- var reverse4d = ReverseOps.reverse4d;
- var slice = SliceOps.slice;
- var slice1d = SliceOps.slice1d;
- var slice2d = SliceOps.slice2d;
- var slice3d = SliceOps.slice3d;
- var slice4d = SliceOps.slice4d;
- var stridedSlice = StridedSliceOps.stridedSlice;
- var argMax = ReductionOps.argMax;
- var argMin = ReductionOps.argMin;
- var logSumExp = ReductionOps.logSumExp;
- var max = ReductionOps.max;
- var mean = ReductionOps.mean;
- var min = ReductionOps.min;
- var moments = ReductionOps.moments;
- var sum = ReductionOps.sum;
- var unsortedSegmentSum = ReductionOps.unsortedSegmentSum;
- var equal = CompareOps.equal;
- var equalStrict = CompareOps.equalStrict;
- var greater = CompareOps.greater;
- var greaterStrict = CompareOps.greaterStrict;
- var greaterEqual = CompareOps.greaterEqual;
- var greaterEqualStrict = CompareOps.greaterEqualStrict;
- var less = CompareOps.less;
- var lessStrict = CompareOps.lessStrict;
- var lessEqual = CompareOps.lessEqual;
- var lessEqualStrict = CompareOps.lessEqualStrict;
- var notEqual = CompareOps.notEqual;
- var notEqualStrict = CompareOps.notEqualStrict;
- var logicalNot = LogicalOps.logicalNot;
- var logicalAnd = LogicalOps.logicalAnd;
- var logicalOr = LogicalOps.logicalOr;
- var logicalXor = LogicalOps.logicalXor;
- var where = LogicalOps.where;
- var abs = UnaryOps.abs;
- var acos = UnaryOps.acos;
- var acosh = UnaryOps.acosh;
- var asin = UnaryOps.asin;
- var asinh = UnaryOps.asinh;
- var atan = UnaryOps.atan;
- var atanh = UnaryOps.atanh;
- var ceil = UnaryOps.ceil;
- var clipByValue = UnaryOps.clipByValue;
- var cos = UnaryOps.cos;
- var cosh = UnaryOps.cosh;
- var elu = UnaryOps.elu;
- var exp = UnaryOps.exp;
- var expm1 = UnaryOps.expm1;
- var floor = UnaryOps.floor;
- var sign = UnaryOps.sign;
- var leakyRelu = UnaryOps.leakyRelu;
- var log = UnaryOps.log;
- var log1p = UnaryOps.log1p;
- var logSigmoid = UnaryOps.logSigmoid;
- var neg = UnaryOps.neg;
- var prelu = UnaryOps.prelu;
- var relu = UnaryOps.relu;
- var reciprocal = UnaryOps.reciprocal;
- var round = UnaryOps.round;
- var selu = UnaryOps.selu;
- var sigmoid = UnaryOps.sigmoid;
- var sin = UnaryOps.sin;
- var sinh = UnaryOps.sinh;
- var softplus = UnaryOps.softplus;
- var sqrt = UnaryOps.sqrt;
- var rsqrt = UnaryOps.rsqrt;
- var square = UnaryOps.square;
- var step = UnaryOps.step;
- var tan = UnaryOps.tan;
- var tanh$1 = UnaryOps.tanh;
- var erf = UnaryOps.erf;
- var add = BinaryOps.add;
- var addStrict = BinaryOps.addStrict;
- var atan2 = BinaryOps.atan2;
- var div = BinaryOps.div;
- var divStrict = BinaryOps.divStrict;
- var maximum = BinaryOps.maximum;
- var maximumStrict = BinaryOps.maximumStrict;
- var minimum = BinaryOps.minimum;
- var minimumStrict = BinaryOps.minimumStrict;
- var mod = BinaryOps.mod;
- var modStrict = BinaryOps.modStrict;
- var mul = BinaryOps.mul;
- var mulStrict = BinaryOps.mulStrict;
- var pow = BinaryOps.pow;
- var powStrict = BinaryOps.powStrict;
- var sub = BinaryOps.sub;
- var subStrict = BinaryOps.subStrict;
- var squaredDifference = BinaryOps.squaredDifference;
- var squaredDifferenceStrict = BinaryOps.squaredDifferenceStrict;
- var norm = NormOps.norm;
- var cast = ArrayOps.cast;
- var clone = ArrayOps.clone;
- var fromPixels = ArrayOps.fromPixels;
- var toPixels = ArrayOps.toPixels;
- var ones = ArrayOps.ones;
- var onesLike = ArrayOps.onesLike;
- var zeros = ArrayOps.zeros;
- var zerosLike = ArrayOps.zerosLike;
- var eye = ArrayOps.eye;
- var rand = ArrayOps.rand;
- var randomNormal = ArrayOps.randomNormal;
- var truncatedNormal = ArrayOps.truncatedNormal;
- var randomUniform = ArrayOps.randomUniform;
- var multinomial = ArrayOps.multinomial;
- var reshape = ArrayOps.reshape;
- var squeeze = ArrayOps.squeeze;
- var tile = ArrayOps.tile;
- var gather = ArrayOps.gather;
- var oneHot = ArrayOps.oneHot;
- var linspace = ArrayOps.linspace;
- var range = ArrayOps.range;
- var buffer = ArrayOps.buffer;
- var fill = ArrayOps.fill;
- var tensor = ArrayOps.tensor;
- var scalar = ArrayOps.scalar;
- var tensor1d = ArrayOps.tensor1d;
- var tensor2d = ArrayOps.tensor2d;
- var tensor3d = ArrayOps.tensor3d;
- var tensor4d = ArrayOps.tensor4d;
- var print = ArrayOps.print;
- var expandDims = ArrayOps.expandDims;
- var stack = ArrayOps.stack;
- var unstack = ArrayOps.unstack;
- var split = ArrayOps.split;
- var cumsum = ArrayOps.cumsum;
- var pad = ArrayOps.pad;
- var pad1d = ArrayOps.pad1d;
- var pad2d = ArrayOps.pad2d;
- var pad3d = ArrayOps.pad3d;
- var pad4d = ArrayOps.pad4d;
- var movingAverage = MovingAverageOps.movingAverage;
- var basicLSTMCell = LSTMOps.basicLSTMCell;
- var multiRNNCell = LSTMOps.multiRNNCell;
- var softmax = SoftmaxOps.softmax;
- var localResponseNormalization = LRNOps.localResponseNormalization;
- var linalg = LinalgOps;
- var losses = {
- absoluteDifference: LossOps.absoluteDifference,
- computeWeightedLoss: LossOps.computeWeightedLoss,
- cosineDistance: LossOps.cosineDistance,
- hingeLoss: LossOps.hingeLoss,
- logLoss: LossOps.logLoss,
- meanSquaredError: LossOps.meanSquaredError,
- softmaxCrossEntropy: SoftmaxOps.softmaxCrossEntropy
- };
- var image = {
- resizeBilinear: ImageOps.resizeBilinear,
- resizeNearestNeighbor: ImageOps.resizeNearestNeighbor,
- };
-
- var __extends = (undefined && undefined.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- var __decorate$o = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step$$1(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step$$1(generator["throw"](value)); } catch (e) { reject(e); } }
- function step$$1(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step$$1((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$1 = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step$$1([n, v]); }; }
- function step$$1(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var TensorBuffer = (function () {
- function TensorBuffer(shape, dtype, values) {
- this.dtype = dtype;
- if (values != null) {
- var n = values.length;
- var size = sizeFromShape(shape);
- assert(n === size, "Length of values '" + n + "' does not match the size " +
- ("inferred by the shape '" + size + "'"));
- }
- this.shape = shape.slice();
- this.values =
- values || getTypedArrayFromDType(dtype, sizeFromShape(shape));
- this.strides = computeStrides(shape);
- this.size = sizeFromShape(shape);
- }
- TensorBuffer.prototype.set = function (value) {
- var locs = [];
- for (var _i = 1; _i < arguments.length; _i++) {
- locs[_i - 1] = arguments[_i];
- }
- if (locs.length === 0) {
- locs = [0];
- }
- assert(locs.length === this.rank, "The number of provided coordinates (" + locs.length + ") must " +
- ("match the rank (" + this.rank + ")"));
- var index = this.locToIndex(locs);
- this.values[index] = value;
- };
- TensorBuffer.prototype.get = function () {
- var locs = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- locs[_i] = arguments[_i];
- }
- if (locs.length === 0) {
- locs = [0];
- }
- var index = locs[locs.length - 1];
- for (var i = 0; i < locs.length - 1; ++i) {
- index += this.strides[i] * locs[i];
- }
- return this.values[index];
- };
- TensorBuffer.prototype.locToIndex = function (locs) {
- if (this.rank === 0) {
- return 0;
- }
- else if (this.rank === 1) {
- return locs[0];
- }
- var index = locs[locs.length - 1];
- for (var i = 0; i < locs.length - 1; ++i) {
- index += this.strides[i] * locs[i];
- }
- return index;
- };
- TensorBuffer.prototype.indexToLoc = function (index) {
- if (this.rank === 0) {
- return [];
- }
- else if (this.rank === 1) {
- return [index];
- }
- var locs = new Array(this.shape.length);
- for (var i = 0; i < locs.length - 1; ++i) {
- locs[i] = Math.floor(index / this.strides[i]);
- index -= locs[i] * this.strides[i];
- }
- locs[locs.length - 1] = index;
- return locs;
- };
- Object.defineProperty(TensorBuffer.prototype, "rank", {
- get: function () {
- return this.shape.length;
- },
- enumerable: true,
- configurable: true
- });
- TensorBuffer.prototype.toTensor = function () {
- return Tensor.make(this.shape, { values: this.values }, this.dtype);
- };
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], TensorBuffer.prototype, "set", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], TensorBuffer.prototype, "get", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], TensorBuffer.prototype, "toTensor", null);
- TensorBuffer = __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], TensorBuffer);
- return TensorBuffer;
- }());
- var Tensor = (function () {
- function Tensor(shape, dtype, values, dataId) {
- this.isDisposed = false;
- this.size = sizeFromShape(shape);
- if (values != null) {
- assert(this.size === values.length, "Constructing tensor of shape (" + this.size + ") should match the " +
- ("length of values (" + values.length + ")"));
- }
- this.shape = shape.slice();
- this.dtype = dtype || 'float32';
- this.strides = computeStrides(shape);
- this.dataId = dataId != null ? dataId : {};
- this.id = Tensor_1.nextId++;
- this.rankType = (this.rank < 5 ? this.rank.toString() : 'higher');
- ENV.engine.registerTensor(this);
- if (values != null) {
- ENV.engine.write(this.dataId, values);
- }
- }
- Tensor_1 = Tensor;
- Tensor.make = function (shape, data, dtype) {
- return new Tensor_1(shape, dtype, data.values, data.dataId);
- };
- Tensor.prototype.flatten = function () {
- this.throwIfDisposed();
- return this.as1D();
- };
- Tensor.prototype.asScalar = function () {
- this.throwIfDisposed();
- assert(this.size === 1, 'The array must have only 1 element.');
- return this.reshape([]);
- };
- Tensor.prototype.as1D = function () {
- this.throwIfDisposed();
- return this.reshape([this.size]);
- };
- Tensor.prototype.as2D = function (rows, columns) {
- this.throwIfDisposed();
- return this.reshape([rows, columns]);
- };
- Tensor.prototype.as3D = function (rows, columns, depth) {
- this.throwIfDisposed();
- return this.reshape([rows, columns, depth]);
- };
- Tensor.prototype.as4D = function (rows, columns, depth, depth2) {
- this.throwIfDisposed();
- return this.reshape([rows, columns, depth, depth2]);
- };
- Tensor.prototype.asType = function (dtype) {
- this.throwIfDisposed();
- return cast(this, dtype);
- };
- Object.defineProperty(Tensor.prototype, "rank", {
- get: function () {
- return this.shape.length;
- },
- enumerable: true,
- configurable: true
- });
- Tensor.prototype.get = function () {
- var locs = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- locs[_i] = arguments[_i];
- }
- assert(locs.length === this.rank, 'Number of coordinates in get() must match the rank of the tensor');
- this.throwIfDisposed();
- if (locs.length === 0) {
- locs = [0];
- }
- var index = locs[locs.length - 1];
- for (var i = 0; i < locs.length - 1; ++i) {
- index += this.strides[i] * locs[i];
- }
- return this.dataSync()[index];
- };
- Tensor.prototype.buffer = function () {
- return buffer(this.shape, this.dtype, this.dataSync());
- };
- Tensor.prototype.data = function () {
- return __awaiter$1(this, void 0, void 0, function () {
- return __generator$1(this, function (_a) {
- this.throwIfDisposed();
- return [2, ENV.engine.read(this.dataId)];
- });
- });
- };
- Tensor.prototype.dataSync = function () {
- this.throwIfDisposed();
- return ENV.engine.readSync(this.dataId);
- };
- Tensor.prototype.dispose = function () {
- if (this.isDisposed) {
- return;
- }
- this.isDisposed = true;
- ENV.engine.disposeTensor(this);
- };
- Tensor.prototype.throwIfDisposed = function () {
- if (this.isDisposed) {
- throw new Error("Tensor is disposed.");
- }
- };
- Tensor.prototype.toFloat = function () {
- return this.asType('float32');
- };
- Tensor.prototype.toInt = function () {
- return this.asType('int32');
- };
- Tensor.prototype.toBool = function () {
- return this.asType('bool');
- };
- Tensor.prototype.print = function (verbose) {
- if (verbose === void 0) { verbose = false; }
- return print(this, verbose);
- };
- Tensor.prototype.reshape = function (newShape) {
- this.throwIfDisposed();
- return reshape(this, newShape);
- };
- Tensor.prototype.reshapeAs = function (x) {
- this.throwIfDisposed();
- return this.reshape(x.shape);
- };
- Tensor.prototype.expandDims = function (axis) {
- if (axis === void 0) { axis = 0; }
- return expandDims(this, axis);
- };
- Tensor.prototype.cumsum = function (axis, exclusive, reverse$$1) {
- if (axis === void 0) { axis = 0; }
- if (exclusive === void 0) { exclusive = false; }
- if (reverse$$1 === void 0) { reverse$$1 = false; }
- return cumsum(this, axis, exclusive, reverse$$1);
- };
- Tensor.prototype.squeeze = function (axis) {
- this.throwIfDisposed();
- return squeeze(this, axis);
- };
- Tensor.prototype.clone = function () {
- this.throwIfDisposed();
- return clone(this);
- };
- Tensor.prototype.toString = function (verbose) {
- if (verbose === void 0) { verbose = false; }
- return tensorToString(this, verbose);
- };
- Tensor.prototype.tile = function (reps) {
- this.throwIfDisposed();
- return tile(this, reps);
- };
- Tensor.prototype.gather = function (indices, axis) {
- if (axis === void 0) { axis = 0; }
- this.throwIfDisposed();
- return gather(this, indices, axis);
- };
- Tensor.prototype.matMul = function (b, transposeA, transposeB) {
- if (transposeA === void 0) { transposeA = false; }
- if (transposeB === void 0) { transposeB = false; }
- this.throwIfDisposed();
- return matMul(this, b, transposeA, transposeB);
- };
- Tensor.prototype.norm = function (ord, axis, keepDims) {
- if (ord === void 0) { ord = 'euclidean'; }
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- this.throwIfDisposed();
- return norm(this, ord, axis, keepDims);
- };
- Tensor.prototype.slice = function (begin, size) {
- this.throwIfDisposed();
- return slice(this, begin, size);
- };
- Tensor.prototype.reverse = function (axis) {
- this.throwIfDisposed();
- return reverse(this, axis);
- };
- Tensor.prototype.concat = function (x, axis) {
- if (axis === void 0) { axis = 0; }
- this.throwIfDisposed();
- return concat([this, x], axis);
- };
- Tensor.prototype.stack = function (x, axis) {
- if (axis === void 0) { axis = 0; }
- return stack([this, x], axis);
- };
- Tensor.prototype.unstack = function (x, axis) {
- if (axis === void 0) { axis = 0; }
- return unstack(this, axis);
- };
- Tensor.prototype.pad = function (paddings, constantValue) {
- if (constantValue === void 0) { constantValue = 0; }
- return pad(this, paddings, constantValue);
- };
- Tensor.prototype.batchNormalization = function (mean$$1, variance, varianceEpsilon, scale, offset) {
- if (varianceEpsilon === void 0) { varianceEpsilon = .001; }
- this.throwIfDisposed();
- return batchNormalization(this, mean$$1, variance, varianceEpsilon, scale, offset);
- };
- Tensor.prototype.logSumExp = function (axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- this.throwIfDisposed();
- return logSumExp(this, axis, keepDims);
- };
- Tensor.prototype.sum = function (axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- this.throwIfDisposed();
- return sum(this, axis, keepDims);
- };
- Tensor.prototype.mean = function (axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- this.throwIfDisposed();
- return mean(this, axis, keepDims);
- };
- Tensor.prototype.min = function (axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- this.throwIfDisposed();
- return min(this, axis, keepDims);
- };
- Tensor.prototype.max = function (axis, keepDims) {
- if (axis === void 0) { axis = null; }
- if (keepDims === void 0) { keepDims = false; }
- this.throwIfDisposed();
- return max(this, axis, keepDims);
- };
- Tensor.prototype.argMin = function (axis) {
- if (axis === void 0) { axis = null; }
- this.throwIfDisposed();
- return argMin(this, axis);
- };
- Tensor.prototype.argMax = function (axis) {
- if (axis === void 0) { axis = null; }
- this.throwIfDisposed();
- return argMax(this, axis);
- };
- Tensor.prototype.cast = function (dtype) {
- this.throwIfDisposed();
- return cast(this, dtype);
- };
- Tensor.prototype.add = function (x) {
- this.throwIfDisposed();
- return add(this, x);
- };
- Tensor.prototype.addStrict = function (x) {
- this.throwIfDisposed();
- return addStrict(this, x);
- };
- Tensor.prototype.sub = function (x) {
- this.throwIfDisposed();
- return sub(this, x);
- };
- Tensor.prototype.subStrict = function (x) {
- this.throwIfDisposed();
- return subStrict(this, x);
- };
- Tensor.prototype.pow = function (exp$$1) {
- this.throwIfDisposed();
- return pow(this, exp$$1);
- };
- Tensor.prototype.powStrict = function (exp$$1) {
- this.throwIfDisposed();
- return powStrict(this, exp$$1);
- };
- Tensor.prototype.mul = function (x) {
- this.throwIfDisposed();
- return mul(this, x);
- };
- Tensor.prototype.mulStrict = function (x) {
- this.throwIfDisposed();
- return mulStrict(this, x);
- };
- Tensor.prototype.div = function (x) {
- this.throwIfDisposed();
- return div(this, x);
- };
- Tensor.prototype.divStrict = function (x) {
- this.throwIfDisposed();
- return divStrict(this, x);
- };
- Tensor.prototype.minimum = function (x) {
- this.throwIfDisposed();
- return minimum(this, x);
- };
- Tensor.prototype.minimumStrict = function (x) {
- this.throwIfDisposed();
- return minimumStrict(this, x);
- };
- Tensor.prototype.maximum = function (x) {
- this.throwIfDisposed();
- return maximum(this, x);
- };
- Tensor.prototype.maximumStrict = function (x) {
- this.throwIfDisposed();
- return maximumStrict(this, x);
- };
- Tensor.prototype.mod = function (x) {
- this.throwIfDisposed();
- return mod(this, x);
- };
- Tensor.prototype.modStrict = function (x) {
- this.throwIfDisposed();
- return modStrict(this, x);
- };
- Tensor.prototype.squaredDifference = function (x) {
- this.throwIfDisposed();
- return squaredDifference(this, x);
- };
- Tensor.prototype.squaredDifferenceStrict = function (x) {
- this.throwIfDisposed();
- return squaredDifferenceStrict(this, x);
- };
- Tensor.prototype.transpose = function (perm) {
- this.throwIfDisposed();
- return transpose(this, perm);
- };
- Tensor.prototype.notEqual = function (x) {
- this.throwIfDisposed();
- return notEqual(this, x);
- };
- Tensor.prototype.notEqualStrict = function (x) {
- this.throwIfDisposed();
- return notEqualStrict(this, x);
- };
- Tensor.prototype.less = function (x) {
- this.throwIfDisposed();
- return less(this, x);
- };
- Tensor.prototype.lessStrict = function (x) {
- this.throwIfDisposed();
- return lessStrict(this, x);
- };
- Tensor.prototype.equal = function (x) {
- this.throwIfDisposed();
- return equal(this, x);
- };
- Tensor.prototype.equalStrict = function (x) {
- this.throwIfDisposed();
- return equalStrict(this, x);
- };
- Tensor.prototype.lessEqual = function (x) {
- this.throwIfDisposed();
- return lessEqual(this, x);
- };
- Tensor.prototype.lessEqualStrict = function (x) {
- this.throwIfDisposed();
- return lessEqualStrict(this, x);
- };
- Tensor.prototype.greater = function (x) {
- this.throwIfDisposed();
- return greater(this, x);
- };
- Tensor.prototype.greaterStrict = function (x) {
- this.throwIfDisposed();
- return greaterStrict(this, x);
- };
- Tensor.prototype.greaterEqual = function (x) {
- this.throwIfDisposed();
- return greaterEqual(this, x);
- };
- Tensor.prototype.greaterEqualStrict = function (x) {
- this.throwIfDisposed();
- return greaterEqualStrict(this, x);
- };
- Tensor.prototype.logicalAnd = function (x) {
- this.throwIfDisposed();
- return logicalAnd(this, x);
- };
- Tensor.prototype.logicalOr = function (x) {
- this.throwIfDisposed();
- return logicalOr(this, x);
- };
- Tensor.prototype.logicalNot = function () {
- this.throwIfDisposed();
- return logicalNot(this);
- };
- Tensor.prototype.logicalXor = function (x) {
- this.throwIfDisposed();
- return logicalXor(this, x);
- };
- Tensor.prototype.where = function (condition, x) {
- this.throwIfDisposed();
- return where(condition, this, x);
- };
- Tensor.prototype.neg = function () {
- this.throwIfDisposed();
- return neg(this);
- };
- Tensor.prototype.ceil = function () {
- this.throwIfDisposed();
- return ceil(this);
- };
- Tensor.prototype.floor = function () {
- this.throwIfDisposed();
- return floor(this);
- };
- Tensor.prototype.sign = function () {
- this.throwIfDisposed();
- return sign(this);
- };
- Tensor.prototype.exp = function () {
- this.throwIfDisposed();
- return exp(this);
- };
- Tensor.prototype.expm1 = function () {
- this.throwIfDisposed();
- return expm1(this);
- };
- Tensor.prototype.log = function () {
- this.throwIfDisposed();
- return log(this);
- };
- Tensor.prototype.log1p = function () {
- this.throwIfDisposed();
- return log1p(this);
- };
- Tensor.prototype.sqrt = function () {
- this.throwIfDisposed();
- return sqrt(this);
- };
- Tensor.prototype.rsqrt = function () {
- this.throwIfDisposed();
- return rsqrt(this);
- };
- Tensor.prototype.square = function () {
- this.throwIfDisposed();
- return square(this);
- };
- Tensor.prototype.reciprocal = function () {
- this.throwIfDisposed();
- return reciprocal(this);
- };
- Tensor.prototype.abs = function () {
- this.throwIfDisposed();
- return abs(this);
- };
- Tensor.prototype.clipByValue = function (min$$1, max$$1) {
- this.throwIfDisposed();
- return clipByValue(this, min$$1, max$$1);
- };
- Tensor.prototype.relu = function () {
- this.throwIfDisposed();
- return relu(this);
- };
- Tensor.prototype.elu = function () {
- this.throwIfDisposed();
- return elu(this);
- };
- Tensor.prototype.selu = function () {
- this.throwIfDisposed();
- return selu(this);
- };
- Tensor.prototype.leakyRelu = function (alpha) {
- if (alpha === void 0) { alpha = 0.2; }
- this.throwIfDisposed();
- return leakyRelu(this, alpha);
- };
- Tensor.prototype.prelu = function (alpha) {
- this.throwIfDisposed();
- return prelu(this, alpha);
- };
- Tensor.prototype.sigmoid = function () {
- this.throwIfDisposed();
- return sigmoid(this);
- };
- Tensor.prototype.logSigmoid = function () {
- this.throwIfDisposed();
- return logSigmoid(this);
- };
- Tensor.prototype.softplus = function () {
- this.throwIfDisposed();
- return softplus(this);
- };
- Tensor.prototype.sin = function () {
- this.throwIfDisposed();
- return sin(this);
- };
- Tensor.prototype.cos = function () {
- this.throwIfDisposed();
- return cos(this);
- };
- Tensor.prototype.tan = function () {
- this.throwIfDisposed();
- return tan(this);
- };
- Tensor.prototype.asin = function () {
- this.throwIfDisposed();
- return asin(this);
- };
- Tensor.prototype.acos = function () {
- this.throwIfDisposed();
- return acos(this);
- };
- Tensor.prototype.atan = function () {
- this.throwIfDisposed();
- return atan(this);
- };
- Tensor.prototype.sinh = function () {
- this.throwIfDisposed();
- return sinh(this);
- };
- Tensor.prototype.cosh = function () {
- this.throwIfDisposed();
- return cosh(this);
- };
- Tensor.prototype.tanh = function () {
- this.throwIfDisposed();
- return tanh$1(this);
- };
- Tensor.prototype.asinh = function () {
- this.throwIfDisposed();
- return asinh(this);
- };
- Tensor.prototype.acosh = function () {
- this.throwIfDisposed();
- return acosh(this);
- };
- Tensor.prototype.atanh = function () {
- this.throwIfDisposed();
- return atanh(this);
- };
- Tensor.prototype.erf = function () {
- this.throwIfDisposed();
- return erf(this);
- };
- Tensor.prototype.round = function () {
- this.throwIfDisposed();
- return round(this);
- };
- Tensor.prototype.step = function (alpha) {
- if (alpha === void 0) { alpha = 0.0; }
- this.throwIfDisposed();
- return step(this, alpha);
- };
- Tensor.prototype.softmax = function (dim) {
- if (dim === void 0) { dim = -1; }
- this.throwIfDisposed();
- return softmax(this, dim);
- };
- Tensor.prototype.resizeBilinear = function (newShape2D, alignCorners) {
- if (alignCorners === void 0) { alignCorners = false; }
- this.throwIfDisposed();
- return image.resizeBilinear(this, newShape2D, alignCorners);
- };
- Tensor.prototype.resizeNearestNeighbor = function (newShape2D, alignCorners) {
- if (alignCorners === void 0) { alignCorners = false; }
- this.throwIfDisposed();
- return image.resizeNearestNeighbor(this, newShape2D, alignCorners);
- };
- Tensor.prototype.conv1d = function (filter, stride, pad$$1, dataFormat, dilation, dimRoundingMode) {
- if (dataFormat === void 0) { dataFormat = 'NWC'; }
- if (dilation === void 0) { dilation = 1; }
- this.throwIfDisposed();
- return conv1d(this, filter, stride, pad$$1, dataFormat, dilation, dimRoundingMode);
- };
- Tensor.prototype.conv2d = function (filter, strides, pad$$1, dataFormat, dilations, dimRoundingMode) {
- if (dataFormat === void 0) { dataFormat = 'NHWC'; }
- if (dilations === void 0) { dilations = [1, 1]; }
- this.throwIfDisposed();
- return conv2d(this, filter, strides, pad$$1, dataFormat, dilations, dimRoundingMode);
- };
- Tensor.prototype.conv2dTranspose = function (filter, outputShape, strides, pad$$1, dimRoundingMode) {
- this.throwIfDisposed();
- return conv2dTranspose(this, filter, outputShape, strides, pad$$1, dimRoundingMode);
- };
- Tensor.prototype.depthwiseConv2D = function (filter, strides, pad$$1, dataFormat, dilations, dimRoundingMode) {
- if (dataFormat === void 0) { dataFormat = 'NHWC'; }
- if (dilations === void 0) { dilations = [1, 1]; }
- this.throwIfDisposed();
- return depthwiseConv2d(this, filter, strides, pad$$1, dataFormat, dilations, dimRoundingMode);
- };
- Tensor.prototype.avgPool = function (filterSize, strides, pad$$1, dimRoundingMode) {
- this.throwIfDisposed();
- return avgPool(this, filterSize, strides, pad$$1, dimRoundingMode);
- };
- Tensor.prototype.maxPool = function (filterSize, strides, pad$$1, dimRoundingMode) {
- this.throwIfDisposed();
- return maxPool(this, filterSize, strides, pad$$1, dimRoundingMode);
- };
- Tensor.prototype.localResponseNormalization = function (radius, bias, alpha, beta) {
- if (radius === void 0) { radius = 5; }
- if (bias === void 0) { bias = 1; }
- if (alpha === void 0) { alpha = 1; }
- if (beta === void 0) { beta = 0.5; }
- return localResponseNormalization(this, radius, bias, alpha, beta);
- };
- Tensor.prototype.variable = function (trainable, name, dtype) {
- if (trainable === void 0) { trainable = true; }
- this.throwIfDisposed();
- return Variable.variable(this, trainable, name, dtype);
- };
- Tensor.prototype.unsortedSegmentSum = function (segmentIds, numSegments, axis) {
- if (axis === void 0) { axis = 0; }
- this.throwIfDisposed();
- return unsortedSegmentSum(this, segmentIds, numSegments, axis);
- };
- Tensor.nextId = 0;
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "flatten", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "asScalar", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "as1D", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "as2D", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "as3D", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "as4D", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "asType", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "buffer", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "data", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "dataSync", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "dispose", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "toFloat", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "toInt", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "toBool", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "print", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "reshape", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "reshapeAs", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "expandDims", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "cumsum", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "squeeze", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "clone", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor.prototype, "toString", null);
- Tensor = Tensor_1 = __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Tensor);
- return Tensor;
- var Tensor_1;
- }());
- var Variable = (function (_super) {
- __extends(Variable, _super);
- function Variable(initialValue, trainable, name) {
- if (trainable === void 0) { trainable = true; }
- var _this = _super.call(this, initialValue.shape, initialValue.dtype, null, initialValue.dataId) || this;
- _this.trainable = trainable;
- _this.name = name;
- if (_this.name == null) {
- _this.name = Variable_1.nextVarId.toString();
- Variable_1.nextVarId++;
- }
- ENV.engine.registerVariable(_this);
- return _this;
- }
- Variable_1 = Variable;
- Variable.variable = function (initialValue, trainable, name, dtype) {
- if (trainable === void 0) { trainable = true; }
- if (dtype != null && dtype !== initialValue.dtype) {
- initialValue = initialValue.asType(dtype);
- }
- return new Variable_1(initialValue, trainable, name);
- };
- Variable.prototype.assign = function (newValue) {
- if (newValue.dtype !== this.dtype) {
- throw new Error("dtype of the new value (" + newValue.dtype + ") and " +
- ("previous value (" + this.dtype + ") must match"));
- }
- if (!arraysEqual(newValue.shape, this.shape)) {
- throw new Error("shape of the new value (" + newValue.shape + ") and " +
- ("previous value (" + this.shape + ") must match"));
- }
- ENV.engine.disposeTensor(this);
- this.dataId = newValue.dataId;
- ENV.engine.registerTensor(this);
- };
- Variable.nextVarId = 0;
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Variable.prototype, "assign", null);
- __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Creation' })
- ], Variable, "variable", null);
- Variable = Variable_1 = __decorate$o([
- doc({ heading: 'Tensors', subheading: 'Classes' })
- ], Variable);
- return Variable;
- var Variable_1;
- }(Tensor));
- var variable = Variable.variable;
- function computeStrides(shape) {
- var rank = shape.length;
- if (rank < 2) {
- return [];
- }
- var strides = new Array(rank - 1);
- strides[rank - 2] = shape[rank - 1];
- for (var i = rank - 3; i >= 0; --i) {
- strides[i] = strides[i + 1] * shape[i + 1];
- }
- return strides;
- }
-
- var __decorate$p = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var Gradients = (function () {
- function Gradients() {
- }
- Gradients.gradScope = function (nameOrScopeFn, scopeFn) {
- return tidy(nameOrScopeFn, scopeFn, true);
- };
- Gradients.grad = function (f) {
- assert(isFunction(f), 'The f passed in grad(f) must be a function');
- return function (x, dy) {
- assert(x instanceof Tensor, 'The x passed in grad(f)(x) must be a tensor');
- assert(dy == null || dy instanceof Tensor, 'The dy passed in grad(f)(x, dy) must be a tensor');
- return tidy(function () {
- var _a = ENV.engine.gradients(function () { return f(x); }, [x], dy), value = _a.value, grads$$1 = _a.grads;
- if (dy != null) {
- assertShapesMatch(value.shape, dy.shape, 'The shape of dy passed in grad(f)(x, dy) must match the shape ' +
- 'returned by f(x)');
- }
- checkGrads(grads$$1);
- return grads$$1[0];
- });
- };
- };
- Gradients.grads = function (f) {
- assert(isFunction(f), 'The f passed in grads(f) must be a function');
- return function (args, dy) {
- assert(Array.isArray(args) && args.every(function (arg) { return arg instanceof Tensor; }), 'The args passed in grads(f)(args) must be an array of tensors');
- assert(dy == null || dy instanceof Tensor, 'The dy passed in grads(f)(args, dy) must be a tensor');
- return tidy(function () {
- var _a = ENV.engine.gradients(function () { return f.apply(void 0, args); }, args, dy), value = _a.value, grads$$1 = _a.grads;
- if (dy != null) {
- assertShapesMatch(value.shape, dy.shape, 'The shape of dy passed in grads(f)([x1,...], dy) must ' +
- 'match the shape returned by f([x1,...])');
- }
- checkGrads(grads$$1);
- return grads$$1;
- });
- };
- };
- Gradients.valueAndGrad = function (f) {
- assert(isFunction(f), 'The f passed in valueAndGrad(f) must be a function');
- return function (x, dy) {
- assert(x instanceof Tensor, 'The x passed in valueAndGrad(f)(x) must be a tensor');
- assert(dy == null || dy instanceof Tensor, 'The dy passed in valueAndGrad(f)(x, dy) must be a tensor');
- var _a = ENV.engine.gradients(function () { return f(x); }, [x], dy), grads$$1 = _a.grads, value = _a.value;
- checkGrads(grads$$1);
- return { grad: grads$$1[0], value: value };
- };
- };
- Gradients.valueAndGrads = function (f) {
- assert(isFunction(f), 'The f passed in valueAndGrads(f) must be a function');
- return function (args, dy) {
- assert(Array.isArray(args) && args.every(function (arg) { return arg instanceof Tensor; }), 'The args passed in valueAndGrads(f)(args) must be array of tensors');
- assert(dy == null || dy instanceof Tensor, 'The dy passed in valueAndGrads(f)(args, dy) must be a tensor');
- var res = ENV.engine.gradients(function () { return f.apply(void 0, args); }, args, dy);
- if (dy != null) {
- assertShapesMatch(res.value.shape, dy.shape, 'The shape of dy passed in valueAndGrads(f)([x1,...], dy) must ' +
- 'match the shape returned by f([x1,...])');
- }
- checkGrads(res.grads);
- return res;
- };
- };
- Gradients.variableGrads = function (f, varList) {
- assert(isFunction(f), 'The f passed in variableGrads(f) must be a function');
- assert(varList == null ||
- Array.isArray(varList) && varList.every(function (v) { return v instanceof Variable; }), 'The varList passed in variableGrads(f, varList) must be an array ' +
- 'of variables');
- if (varList == null) {
- varList = [];
- for (var varName in ENV.engine.registeredVariables) {
- varList.push(ENV.engine.registeredVariables[varName]);
- }
- }
- var originalVarCount = varList.length;
- varList = varList.filter(function (variable$$1) { return variable$$1.trainable; });
- assert(varList.length > 0, "variableGrads() expects at least one of the input variables to be " +
- ("trainable, but none of the " + originalVarCount + " variables is ") +
- "trainable.");
- var allowNoGradients = true;
- var _a = ENV.engine.gradients(f, varList, null, allowNoGradients), value = _a.value, grads$$1 = _a.grads;
- assert(grads$$1.some(function (g) { return g != null; }), 'Cannot find a connection between any variable and the result of the ' +
- 'loss function y=f(x). Please make sure the operations that use ' +
- 'variables are inside the function f passed to minimize().');
- assert(value.rank === 0, "The f passed in variableGrads(f) must return a scalar, but it " +
- ("returned a rank-" + value.rank + " tensor"));
- var namedGrads = {};
- varList.forEach(function (v, i) {
- if (grads$$1[i] != null) {
- namedGrads[v.name] = grads$$1[i];
- }
- });
- return { value: value, grads: namedGrads };
- };
- Gradients.customGrad = function (f) {
- return ENV.engine.customGrad(f);
- };
- __decorate$p([
- doc({ heading: 'Training', subheading: 'Gradients' })
- ], Gradients, "grad", null);
- __decorate$p([
- doc({ heading: 'Training', subheading: 'Gradients' })
- ], Gradients, "grads", null);
- __decorate$p([
- doc({ heading: 'Training', subheading: 'Gradients' })
- ], Gradients, "valueAndGrad", null);
- __decorate$p([
- doc({ heading: 'Training', subheading: 'Gradients' })
- ], Gradients, "valueAndGrads", null);
- __decorate$p([
- doc({ heading: 'Training', subheading: 'Gradients' })
- ], Gradients, "variableGrads", null);
- __decorate$p([
- doc({ heading: 'Training', subheading: 'Gradients' })
- ], Gradients, "customGrad", null);
- return Gradients;
- }());
- function checkGrads(grads$$1) {
- var numNullGradients = grads$$1.filter(function (g) { return g == null; }).length;
- if (numNullGradients > 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 tidy = Tracking.tidy;
- var keep = Tracking.keep;
- var dispose = Tracking.dispose;
- var time = Tracking.time;
- var grad = Gradients.grad;
- var valueAndGrad = Gradients.valueAndGrad;
- var grads = Gradients.grads;
- var valueAndGrads = Gradients.valueAndGrads;
- var variableGrads = Gradients.variableGrads;
- var customGrad = Gradients.customGrad;
-
- var Profiler = (function () {
- function Profiler(backendTimer, logger) {
- this.backendTimer = backendTimer;
- this.logger = logger;
- if (logger == null) {
- this.logger = new Logger();
- }
- }
- Profiler.prototype.profileKernel = function (name, f) {
- var _this = this;
- var result;
- var holdResultWrapperFn = function () {
- result = f();
- };
- var timer = this.backendTimer.time(holdResultWrapperFn);
- var vals = result.dataSync();
- checkForNaN(vals, result.dtype, name);
- timer.then(function (timing) {
- _this.logger.logKernelProfile(name, result, vals, timing.kernelMs);
- });
- return result;
- };
- return Profiler;
- }());
- var Logger = (function () {
- function Logger() {
- }
- Logger.prototype.logKernelProfile = function (name, result, vals, timeMs) {
- var time = rightPad(timeMs + "ms", 9);
- var paddedName = rightPad(name, 25);
- var rank = result.rank;
- var size = result.size;
- var shape = rightPad(result.shape.toString(), 14);
- console.log("%c" + paddedName + "\t%c" + time + "\t%c" + rank + "D " + shape + "\t%c" + size, 'font-weight:bold', 'color:red', 'color:blue', 'color: orange');
- };
- return Logger;
- }());
-
- function getFilteredNodesXToY(tape, xs, y) {
- var tensorsFromX = {};
- var nodesFromX = {};
- for (var i = 0; i < xs.length; i++) {
- tensorsFromX[xs[i].id] = true;
- }
- for (var i = 0; i < tape.length; i++) {
- var node = tape[i];
- var nodeInputs = node.inputs;
- for (var inputName in nodeInputs) {
- var input = nodeInputs[inputName];
- var anyInputFromX = false;
- for (var j = 0; j < xs.length; j++) {
- if (tensorsFromX[input.id]) {
- tensorsFromX[node.output.id] = true;
- anyInputFromX = true;
- nodesFromX[node.id] = true;
- break;
- }
- }
- if (anyInputFromX) {
- break;
- }
- }
- }
- var tensorsLeadToY = {};
- tensorsLeadToY[y.id] = true;
- var nodesToY = {};
- for (var i = tape.length - 1; i >= 0; i--) {
- var node = tape[i];
- var nodeInputs = node.inputs;
- var outputs = [];
- outputs.push(node.output);
- for (var j = 0; j < outputs.length; j++) {
- if (tensorsLeadToY[outputs[j].id]) {
- for (var inputName in nodeInputs) {
- tensorsLeadToY[nodeInputs[inputName].id] = true;
- nodesToY[node.id] = true;
- }
- break;
- }
- }
- }
- var filteredTape = [];
- for (var i = 0; i < tape.length; i++) {
- var node = tape[i];
- if (nodesFromX[node.id] && nodesToY[node.id]) {
- var prunedInputs = {};
- for (var inputName in node.inputs) {
- var nodeInput = node.inputs[inputName];
- if (tensorsFromX[nodeInput.id]) {
- prunedInputs[inputName] = nodeInput;
- }
- }
- var prunedNode = Object.assign({}, node);
- prunedNode.inputs = prunedInputs;
- prunedNode.output = node.output;
- filteredTape.push(prunedNode);
- }
- }
- return filteredTape;
- }
- function backpropagateGradients(tensorAccumulatedGradientMap, filteredTape) {
- for (var i = filteredTape.length - 1; i >= 0; i--) {
- var node = filteredTape[i];
- var dy = tensorAccumulatedGradientMap[node.output.id];
- if (node.gradient == null) {
- throw new Error("Cannot compute gradient: gradient function not found " +
- ("for " + node.name + "."));
- }
- var inputGradients = node.gradient(dy);
- for (var inputName in node.inputs) {
- if (!(inputName in inputGradients)) {
- throw new Error("Cannot backprop through input " + inputName + ". " +
- ("Available gradients found: " + Object.keys(inputGradients) + "."));
- }
- var dx = inputGradients[inputName]();
- var x = node.inputs[inputName];
- if (!arraysEqual(dx.shape, x.shape)) {
- throw new Error("Error in gradient for op " + node.name + ". The gradient of input " +
- ("'" + inputName + "' has shape '" + dx.shape + "', which does not match ") +
- ("the shape of the input '" + x.shape + "'"));
- }
- if (tensorAccumulatedGradientMap[x.id] == null) {
- tensorAccumulatedGradientMap[x.id] = dx;
- }
- else {
- var curGradient = tensorAccumulatedGradientMap[x.id];
- tensorAccumulatedGradientMap[x.id] = curGradient.add(dx);
- curGradient.dispose();
- }
- }
- }
- }
-
- var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step$$1(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step$$1(generator["throw"](value)); } catch (e) { reject(e); } }
- function step$$1(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step$$1((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$2 = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step$$1([n, v]); }; }
- function step$$1(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var Engine = (function () {
- function Engine(backend, safeMode) {
- this.backend = backend;
- this.safeMode = safeMode;
- this.registeredVariables = {};
- this.refCounter = new WeakMap();
- this.nextTapeNodeId = 0;
- this.numBytes = 0;
- this.numTensors = 0;
- this.numDataBuffers = 0;
- this.gradientScopeCount = 0;
- this.customGradientDepth = 0;
- this.activeScope = { keep: [], track: [] };
- this.scopeStack = [this.activeScope];
- this.profiler = new Profiler(backend);
- }
- Engine.prototype.runKernel = function (forwardFunc, inputs, backwardsFunc) {
- var _this = this;
- var result;
- var saved = [];
- var saveFunc = function (x) {
- saved.push(x);
- return x;
- };
- var scopeName = this.activeScope.name;
- this.customGradientDepth++;
- if (!ENV.get('DEBUG')) {
- result = forwardFunc(this.backend, saveFunc);
- }
- else {
- result = this.profiler.profileKernel(scopeName, function () { return forwardFunc(_this.backend, saveFunc); });
- }
- this.customGradientDepth--;
- if (this.shouldRecord()) {
- var tapeNode = {
- id: this.nextTapeNodeId++,
- name: scopeName,
- inputs: inputs,
- output: result,
- };
- if (backwardsFunc != null) {
- tapeNode.gradient = function (dy) { return backwardsFunc(dy, saved); };
- }
- this.activeTape.push(tapeNode);
- }
- return result;
- };
- Engine.prototype.registerTensor = function (a) {
- var refCount = this.refCounter.has(a.dataId) ? this.refCounter.get(a.dataId) : 0;
- this.numTensors++;
- if (refCount === 0) {
- this.numDataBuffers++;
- this.numBytes +=
- sizeFromShape(a.shape) * bytesPerElement(a.dtype);
- this.backend.register(a.dataId, a.shape, a.dtype);
- }
- this.refCounter.set(a.dataId, refCount + 1);
- if (!(a instanceof Variable)) {
- this.track(a);
- }
- };
- Engine.prototype.registerVariable = function (v) {
- if (this.registeredVariables[v.name] != null) {
- throw new Error("Variable with name " + v.name + " was already registered");
- }
- this.registeredVariables[v.name] = v;
- };
- Engine.prototype.disposeTensor = function (a) {
- if (!this.refCounter.has(a.dataId)) {
- return;
- }
- this.numTensors--;
- var refCount = this.refCounter.get(a.dataId);
- if (refCount <= 1) {
- this.refCounter.delete(a.dataId);
- this.backend.disposeData(a.dataId);
- this.numDataBuffers--;
- this.numBytes -=
- sizeFromShape(a.shape) * bytesPerElement(a.dtype);
- }
- else {
- this.refCounter.set(a.dataId, refCount - 1);
- }
- };
- Engine.prototype.disposeVariables = function () {
- for (var varName in this.registeredVariables) {
- var v = this.registeredVariables[varName];
- this.disposeTensor(v);
- delete this.registeredVariables[varName];
- }
- };
- Engine.prototype.memory = function () {
- var info = this.backend.memory();
- info.numTensors = this.numTensors;
- info.numDataBuffers = this.numDataBuffers;
- info.numBytes = this.numBytes;
- return info;
- };
- Engine.prototype.shouldRecord = function () {
- return this.activeTape != null && this.customGradientDepth === 0;
- };
- Engine.prototype.addTapeNode = function (inputs, result, gradientsFunc) {
- var inputsMap = {};
- inputs.forEach(function (input, idx) {
- inputsMap[idx] = input;
- });
- var gradient = function (dy) {
- var res = gradientsFunc(dy);
- var resMap = {};
- res.forEach(function (r, idx) {
- resMap[idx] = function () { return r; };
- });
- return resMap;
- };
- var tapeNode = {
- id: this.nextTapeNodeId++,
- name: this.activeScope.name,
- inputs: inputsMap,
- output: result,
- gradient: gradient
- };
- this.activeTape.push(tapeNode);
- };
- Engine.prototype.keep = function (result) {
- if (this.scopeStack.length === 1 && ENV.engine.safeMode) {
- throw new Error('Safe mode is ON. Enclose all tensor operations inside tf.tidy(): ' +
- 'tf.tidy(() => {...}) to avoid memory leaks.');
- }
- this.activeScope.keep.push(result);
- return result;
- };
- Engine.prototype.startScope = function (name, gradientsMode) {
- if (gradientsMode === void 0) { gradientsMode = false; }
- if (gradientsMode && this.gradientScopeCount === 0) {
- this.activeTape = [];
- }
- if (gradientsMode) {
- this.gradientScopeCount++;
- }
- var scopeInfo = { keep: [], track: [] };
- if (name) {
- scopeInfo.name = name;
- }
- this.scopeStack.push(scopeInfo);
- this.activeScope = scopeInfo;
- };
- Engine.prototype.endScope = function (result, gradientsMode) {
- var _this = this;
- if (gradientsMode === void 0) { gradientsMode = false; }
- if (gradientsMode) {
- this.gradientScopeCount--;
- if (this.gradientScopeCount === 0) {
- this.activeTape = null;
- }
- }
- var tensorsToKeep = this.activeScope.keep;
- var tensorsToTrackInParent = extractTensorsFromContainer(result);
- tensorsToKeep = tensorsToKeep.concat(tensorsToTrackInParent);
- for (var i = 0; i < this.activeScope.track.length; i++) {
- var tensor$$1 = this.activeScope.track[i];
- if (isTensorInList(tensor$$1, tensorsToKeep)) {
- continue;
- }
- if (this.activeTape != null) {
- tensorsToTrackInParent.push(tensor$$1);
- }
- else {
- tensor$$1.dispose();
- }
- }
- this.scopeStack.pop();
- this.activeScope = this.scopeStack.length === 0 ?
- { keep: [], track: [] } :
- this.scopeStack[this.scopeStack.length - 1];
- tensorsToTrackInParent.forEach(function (tensor$$1) {
- if (!isTensorInList(tensor$$1, _this.activeScope.keep)) {
- _this.track(tensor$$1);
- }
- });
- };
- Engine.prototype.dispose = function () { };
- Engine.prototype.gradients = function (f, xs, dy, allowNoGradients) {
- var _this = this;
- if (allowNoGradients === void 0) { allowNoGradients = false; }
- assert(xs.length > 0, 'gradients() received an empty list of xs.');
- return tidy('gradients', function () {
- var y = f();
- assert(y instanceof Tensor, 'The result y returned by f() must be a tensor.');
- var filteredTape = getFilteredNodesXToY(_this.activeTape, xs, y);
- if (!allowNoGradients && filteredTape.length === 0 && xs.length > 0) {
- throw new Error('Cannot compute gradient of y=f(x) with respect to x. Make sure ' +
- 'that the f you passed encloses all operations that lead from x ' +
- 'to y.');
- }
- var accumulatedGradientMap = {};
- accumulatedGradientMap[y.id] = (dy == null) ? ones(y.shape) : dy;
- backpropagateGradients(accumulatedGradientMap, filteredTape);
- var grads$$1 = xs.map(function (x) { return accumulatedGradientMap[x.id]; });
- return { value: y, grads: grads$$1 };
- }, true);
- };
- Engine.prototype.customGrad = function (f) {
- var _this = this;
- assert(isFunction(f), 'The f passed in customGrad(f) must be a function.');
- return function () {
- var inputs = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- inputs[_i] = arguments[_i];
- }
- assert(inputs.every(function (t) { return t instanceof Tensor; }), 'The args passed in customGrad(f)(x1, x2,...) must all be tensors');
- _this.customGradientDepth++;
- var gradientsFunc;
- var gradientsMode = true;
- var result = tidy(f.name, function () {
- var _a = f.apply(void 0, inputs), value = _a.value, gradFunc = _a.gradFunc;
- assert(value instanceof Tensor, 'The function f passed in customGrad(f) must return an object ' +
- 'where `obj.value` is a tensor');
- assert(isFunction(gradFunc), 'The function f passed in customGrad(f) must return an object ' +
- 'where `obj.gradFunc` is a function.');
- gradientsFunc = gradFunc;
- return value;
- }, gradientsMode);
- _this.customGradientDepth--;
- if (_this.shouldRecord()) {
- var gradFunc = function (dy) {
- var res = gradientsFunc(dy);
- var grads$$1 = Array.isArray(res) ? res : [res];
- assert(grads$$1.length === inputs.length, 'The function f passed in customGrad(f) must return an object ' +
- 'where `obj.gradFunc` is a function that returns the same ' +
- 'number of tensors as inputs passed to f(...).');
- assert(grads$$1.every(function (t) { return t instanceof Tensor; }), 'The function f passed in customGrad(f) must return an object ' +
- 'where `obj.gradFunc` is a function that returns a list of ' +
- 'only tensors.');
- return grads$$1;
- };
- _this.addTapeNode(inputs, result, gradFunc);
- }
- return result;
- };
- };
- Engine.prototype.write = function (dataId, values) {
- this.backend.write(dataId, values);
- };
- Engine.prototype.readSync = function (dataId) {
- return this.backend.readSync(dataId);
- };
- Engine.prototype.read = function (dataId) {
- return this.backend.read(dataId);
- };
- Engine.prototype.fromPixels = function (pixels, numChannels) {
- return this.backend.fromPixels(pixels, numChannels);
- };
- Engine.prototype.time = function (query) {
- return __awaiter$2(this, void 0, void 0, function () {
- var start, timingInfo;
- return __generator$2(this, function (_a) {
- switch (_a.label) {
- case 0:
- start = performance.now();
- return [4, this.backend.time(query)];
- case 1:
- timingInfo = _a.sent();
- timingInfo.wallMs = performance.now() - start;
- return [2, timingInfo];
- }
- });
- });
- };
- Engine.prototype.track = function (result) {
- if (this.scopeStack.length === 1 && this.safeMode) {
- throw new Error('Safe mode is ON. Enclose all tensor operations inside tf.tidy(): ' +
- 'tf.tidy(() => {op();...}); to avoid memory leaks.');
- }
- this.activeScope.track.push(result);
- return result;
- };
- return Engine;
- }());
-
- var __decorate$q = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var Type;
- (function (Type) {
- Type[Type["NUMBER"] = 0] = "NUMBER";
- Type[Type["BOOLEAN"] = 1] = "BOOLEAN";
- Type[Type["STRING"] = 2] = "STRING";
- })(Type || (Type = {}));
- var URL_PROPERTIES = [
- { name: 'DEBUG', type: Type.BOOLEAN }, { name: 'IS_BROWSER', type: Type.BOOLEAN },
- { name: 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION', type: Type.NUMBER },
- { name: 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE', type: Type.BOOLEAN },
- { name: 'WEBGL_VERSION', type: Type.NUMBER },
- { name: 'WEBGL_FLOAT_TEXTURE_ENABLED', type: Type.BOOLEAN }, {
- name: 'WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED',
- type: Type.BOOLEAN
- },
- { name: 'BACKEND', type: Type.STRING }
- ];
- function hasExtension(gl, extensionName) {
- var ext = gl.getExtension(extensionName);
- return ext != null;
- }
- function getWebGLRenderingContext(webGLVersion) {
- if (webGLVersion === 0) {
- throw new Error('Cannot get WebGL rendering context, WebGL is disabled.');
- }
- var tempCanvas = document.createElement('canvas');
- if (webGLVersion === 1) {
- return (tempCanvas.getContext('webgl') ||
- tempCanvas.getContext('experimental-webgl'));
- }
- return tempCanvas.getContext('webgl2');
- }
- function loseContext(gl) {
- if (gl != null) {
- var loseContextExtension = gl.getExtension('WEBGL_lose_context');
- if (loseContextExtension == null) {
- throw new Error('Extension WEBGL_lose_context not supported on this browser.');
- }
- loseContextExtension.loseContext();
- }
- }
- function isWebGLVersionEnabled(webGLVersion) {
- var gl = getWebGLRenderingContext(webGLVersion);
- if (gl != null) {
- loseContext(gl);
- return true;
- }
- return false;
- }
- function getWebGLDisjointQueryTimerVersion(webGLVersion) {
- if (webGLVersion === 0) {
- return 0;
- }
- var queryTimerVersion;
- var gl = getWebGLRenderingContext(webGLVersion);
- if (hasExtension(gl, 'EXT_disjoint_timer_query_webgl2') &&
- webGLVersion === 2) {
- queryTimerVersion = 2;
- }
- else if (hasExtension(gl, 'EXT_disjoint_timer_query')) {
- queryTimerVersion = 1;
- }
- else {
- queryTimerVersion = 0;
- }
- if (gl != null) {
- loseContext(gl);
- }
- return queryTimerVersion;
- }
- function isFloatTextureReadPixelsEnabled(webGLVersion) {
- if (webGLVersion === 0) {
- return false;
- }
- var gl = getWebGLRenderingContext(webGLVersion);
- if (webGLVersion === 1) {
- if (!hasExtension(gl, 'OES_texture_float')) {
- return false;
- }
- }
- else {
- if (!hasExtension(gl, 'EXT_color_buffer_float')) {
- return false;
- }
- }
- var frameBuffer = gl.createFramebuffer();
- var texture = gl.createTexture();
- gl.bindTexture(gl.TEXTURE_2D, texture);
- var internalFormat = webGLVersion === 2 ? gl.RGBA32F : gl.RGBA;
- gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, 1, 1, 0, gl.RGBA, gl.FLOAT, null);
- gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
- var frameBufferComplete = (gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE);
- gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.FLOAT, new Float32Array(4));
- var readPixelsNoError = gl.getError() === gl.NO_ERROR;
- loseContext(gl);
- return frameBufferComplete && readPixelsNoError;
- }
- function isWebGLGetBufferSubDataAsyncExtensionEnabled(webGLVersion) {
- if (webGLVersion > 0) {
- return false;
- }
- if (webGLVersion !== 2) {
- return false;
- }
- var gl = getWebGLRenderingContext(webGLVersion);
- var isEnabled = hasExtension(gl, 'WEBGL_get_buffer_sub_data_async');
- loseContext(gl);
- return isEnabled;
- }
- var Environment = (function () {
- function Environment(features) {
- this.features = {};
- this.registry = {};
- if (features != null) {
- this.features = features;
- }
- if (this.get('DEBUG')) {
- console.warn('Debugging mode is ON. The output of every math call will ' +
- 'be downloaded to CPU and checked for NaNs. ' +
- 'This significantly impacts performance.');
- }
- }
- Environment.setBackend = function (backendType, safeMode) {
- if (safeMode === void 0) { safeMode = false; }
- if (!(backendType in ENV.registry)) {
- throw new Error("Backend type '" + backendType + "' not found in registry");
- }
- ENV.initBackend(backendType, safeMode);
- };
- Environment.getBackend = function () {
- ENV.initDefaultBackend();
- return ENV.currentBackend;
- };
- Environment.disposeVariables = function () {
- ENV.engine.disposeVariables();
- };
- Environment.memory = function () {
- return ENV.engine.memory();
- };
- Environment.prototype.get = function (feature) {
- if (feature in this.features) {
- return this.features[feature];
- }
- this.features[feature] = this.evaluateFeature(feature);
- return this.features[feature];
- };
- Environment.prototype.set = function (feature, value) {
- this.features[feature] = value;
- };
- Environment.prototype.getBestBackendType = function () {
- var _this = this;
- if (Object.keys(this.registry).length === 0) {
- throw new Error('No backend found in registry.');
- }
- var sortedBackends = Object.keys(this.registry)
- .map(function (name) {
- return { name: name, entry: _this.registry[name] };
- })
- .sort(function (a, b) {
- return b.entry.priority - a.entry.priority;
- });
- return sortedBackends[0].name;
- };
- Environment.prototype.evaluateFeature = function (feature) {
- if (feature === 'DEBUG') {
- return false;
- }
- else if (feature === 'IS_BROWSER') {
- return typeof window !== 'undefined';
- }
- else if (feature === 'BACKEND') {
- return this.getBestBackendType();
- }
- else if (feature === 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION') {
- var webGLVersion = this.get('WEBGL_VERSION');
- if (webGLVersion === 0) {
- return 0;
- }
- return getWebGLDisjointQueryTimerVersion(webGLVersion);
- }
- else if (feature === 'WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE') {
- return this.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION') > 0 &&
- !isMobile();
- }
- else if (feature === 'WEBGL_VERSION') {
- if (isWebGLVersionEnabled(2)) {
- return 2;
- }
- else if (isWebGLVersionEnabled(1)) {
- return 1;
- }
- return 0;
- }
- else if (feature === 'WEBGL_FLOAT_TEXTURE_ENABLED') {
- return isFloatTextureReadPixelsEnabled(this.get('WEBGL_VERSION'));
- }
- else if (feature === 'WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED') {
- return isWebGLGetBufferSubDataAsyncExtensionEnabled(this.get('WEBGL_VERSION'));
- }
- throw new Error("Unknown feature " + feature + ".");
- };
- Environment.prototype.setFeatures = function (features) {
- this.features = features;
- };
- Environment.prototype.reset = function () {
- this.features = getFeaturesFromURL();
- if (this.globalEngine != null) {
- this.globalEngine.dispose();
- this.globalEngine = null;
- }
- };
- Environment.prototype.initBackend = function (backendType, safeMode) {
- if (safeMode === void 0) { safeMode = false; }
- this.currentBackend = backendType;
- if (this.globalEngine != null) {
- this.globalEngine.dispose();
- }
- var backend = ENV.findBackend(backendType);
- this.globalEngine = new Engine(backend, safeMode);
- };
- Environment.prototype.findBackend = function (name) {
- if (!(name in this.registry)) {
- return null;
- }
- return this.registry[name].backend;
- };
- Environment.prototype.registerBackend = function (name, factory, priority) {
- if (priority === void 0) { priority = 1; }
- if (name in this.registry) {
- console.warn(name + " backend was already registered");
- }
- try {
- var backend = factory();
- this.registry[name] = { backend: backend, priority: priority };
- return true;
- }
- catch (err) {
- console.warn(err.message);
- return false;
- }
- };
- Environment.prototype.removeBackend = function (name) {
- if (!(name in this.registry)) {
- throw new Error(name + " backend not found in registry");
- }
- this.registry[name].backend.dispose();
- delete this.registry[name];
- };
- Object.defineProperty(Environment.prototype, "engine", {
- get: function () {
- this.initDefaultBackend();
- return this.globalEngine;
- },
- enumerable: true,
- configurable: true
- });
- Environment.prototype.initDefaultBackend = function () {
- if (this.globalEngine == null) {
- this.initBackend(ENV.get('BACKEND'), false);
- }
- };
- __decorate$q([
- doc({ heading: 'Environment' })
- ], Environment, "setBackend", null);
- __decorate$q([
- doc({ heading: 'Environment' })
- ], Environment, "getBackend", null);
- __decorate$q([
- doc({ heading: 'Environment' })
- ], Environment, "disposeVariables", null);
- __decorate$q([
- doc({ heading: 'Performance', subheading: 'Memory' })
- ], Environment, "memory", null);
- return Environment;
- }());
- var TENSORFLOWJS_FLAGS_PREFIX = 'tfjsflags';
- function getFeaturesFromURL() {
- var features = {};
- if (typeof window === 'undefined' || typeof window.location === 'undefined') {
- return features;
- }
- var urlParams = getQueryParams(window.location.search);
- if (TENSORFLOWJS_FLAGS_PREFIX in urlParams) {
- var urlFlags_1 = {};
- var keyValues = urlParams[TENSORFLOWJS_FLAGS_PREFIX].split(',');
- keyValues.forEach(function (keyValue) {
- var _a = keyValue.split(':'), key = _a[0], value = _a[1];
- urlFlags_1[key] = value;
- });
- URL_PROPERTIES.forEach(function (urlProperty) {
- if (urlProperty.name in urlFlags_1) {
- console.log("Setting feature override from URL " + urlProperty.name + ": " +
- ("" + urlFlags_1[urlProperty.name]));
- if (urlProperty.type === Type.NUMBER) {
- features[urlProperty.name] = +urlFlags_1[urlProperty.name];
- }
- else if (urlProperty.type === Type.BOOLEAN) {
- features[urlProperty.name] = urlFlags_1[urlProperty.name] === 'true';
- }
- else if (urlProperty.type === Type.STRING) {
- features[urlProperty.name] = urlFlags_1[urlProperty.name];
- }
- else {
- console.warn("Unknown URL param: " + urlProperty.name + ".");
- }
- }
- });
- }
- return features;
- }
- function getGlobalNamespace() {
- var ns;
- if (typeof (window) !== 'undefined') {
- ns = window;
- }
- else if (typeof (global) !== 'undefined') {
- ns = global;
- }
- else {
- throw new Error('Could not find a global object');
- }
- return ns;
- }
- function getOrMakeEnvironment() {
- var ns = getGlobalNamespace();
- ns.ENV = ns.ENV || new Environment(getFeaturesFromURL());
- return ns.ENV;
- }
- var ENV = getOrMakeEnvironment();
-
- var environment = /*#__PURE__*/Object.freeze({
- get Type () { return Type; },
- URL_PROPERTIES: URL_PROPERTIES,
- Environment: Environment,
- ENV: ENV
- });
-
- var PARALLELIZE_THRESHOLD = 30;
- function computeOptimalWindowSize(inSize) {
- if (inSize <= PARALLELIZE_THRESHOLD) {
- return inSize;
- }
- return nearestDivisor(inSize, Math.floor(Math.sqrt(inSize)));
- }
- function nearestDivisor(size, start) {
- for (var i = start; i < size; ++i) {
- if (size % i === 0) {
- return i;
- }
- }
- return size;
- }
-
- function castTensor(x, dtype, backend) {
- if (!hasEncodingLoss(x.dtype, dtype)) {
- return Tensor.make(x.shape, { dataId: x.dataId }, dtype);
- }
- if (dtype === 'int32') {
- return backend.int(x);
- }
- else if (dtype === 'bool') {
- return backend.notEqual(x, ArrayOps.scalar(0, x.dtype));
- }
- else {
- throw new Error("Error in Cast: unknown dtype argument (" + dtype + ")");
- }
- }
- function reshapeTensor(x, shape) {
- return Tensor.make(shape, { dataId: x.dataId }, x.dtype);
- }
-
- var ArgMinMaxProgram = (function () {
- function ArgMinMaxProgram(reduceInfo, op, firstPass) {
- this.variableNames = ['A'];
- var windowSize = reduceInfo.windowSize;
- var batchSize = reduceInfo.batchSize;
- var inSize = reduceInfo.inSize;
- var outSize = Math.ceil(inSize / windowSize);
- if (!firstPass) {
- this.variableNames.push('bestIndicesA');
- }
- this.outputShape = [batchSize, outSize];
- var compOp = (op === 'max') ? '>' : '<';
- var indexSnippet = firstPass ?
- '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 * " + windowSize + ";\n\n int bestIndex = 0;\n float bestValue = getA(batch, inOffset);\n\n for (int i = 0; i < " + windowSize + "; i++) {\n int inIdx = " + indexSnippet + ";\n float candidate = getA(batch, inIdx);\n if (candidate " + compOp + " bestValue) {\n bestValue = candidate;\n bestIndex = inIdx;\n }\n }\n setOutput(float(bestIndex));\n }\n ";
- }
- return ArgMinMaxProgram;
- }());
-
- var AvgPool2DBackpropProgram = (function () {
- function AvgPool2DBackpropProgram(convInfo) {
- this.variableNames = ['dy'];
- this.outputShape = convInfo.inShape;
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var padTop = filterHeight - 1 - convInfo.padInfo.top;
- var padLeft = filterWidth - 1 - convInfo.padInfo.left;
- var avgMultiplier = 1 / (filterHeight * filterWidth);
- this.userCode = "\n const ivec2 pads = ivec2(" + padTop + ", " + padLeft + ");\n const float avgMultiplier = float(" + avgMultiplier + ");\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 < " + filterHeight + "; wR++) {\n float dyR = float(dyRCorner + wR) / " + strideHeight + ".0;\n\n if (dyR < 0.0 || dyR >= " + convInfo.outHeight + ".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < " + filterWidth + "; wC++) {\n float dyC = float(dyCCorner + wC) / " + strideWidth + ".0;\n\n if (dyC < 0.0 || dyC >= " + convInfo.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 ";
- }
- return AvgPool2DBackpropProgram;
- }());
-
- var BatchNormProgram = (function () {
- function BatchNormProgram(xShape, meanShape, varianceShape, offsetShape, scaleShape, varianceEpsilon) {
- this.outputShape = [];
- this.supportsBroadcasting = true;
- this.variableNames = ['x', 'mean', 'variance'];
- assertAndGetBroadcastShape(xShape, meanShape);
- assertAndGetBroadcastShape(xShape, varianceShape);
- var offsetSnippet = '0.0';
- if (offsetShape != null) {
- assertAndGetBroadcastShape(xShape, offsetShape);
- this.variableNames.push('offset');
- offsetSnippet = 'getOffsetAtOutCoords()';
- }
- var scaleSnippet = '1.0';
- if (scaleShape != null) {
- assertAndGetBroadcastShape(xShape, scaleShape);
- this.variableNames.push('scale');
- scaleSnippet = 'getScaleAtOutCoords()';
- }
- this.outputShape = xShape;
- this.userCode = "\n void main() {\n float x = getXAtOutCoords();\n float mean = getMeanAtOutCoords();\n float variance = getVarianceAtOutCoords();\n float offset = " + offsetSnippet + ";\n float scale = " + scaleSnippet + ";\n float inv = scale * inversesqrt(variance + float(" + varianceEpsilon + "));\n setOutput((x - mean) * inv + offset);\n }\n ";
- }
- return BatchNormProgram;
- }());
-
- var CHECK_NAN_SNIPPET = "\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n";
- var ADD = 'return a + b;';
- var SUB = 'return a - b;';
- var MUL = 'return a * b;';
- var DIV = 'return a / b;';
- var INT_DIV = "\n float resultSign = sign(a) * sign(b);\n int ia = round(a);\n int ib = round(b);\n int result = ia / ib;\n int amodb = ia - ib * result;\n\n if (resultSign < 0.0 && amodb != 0) {\n result -= 1;\n }\n return float(result);\n";
- var POW = "\n return (round(mod(b, 2.0)) == 0 || round(mod(b, 2.0)) == 2) ?\n pow(abs(a), b) : sign(a) * pow(abs(a), b);\n";
- var SQUARED_DIFFERENCE = 'return (a - b) * (a - b);';
- var EQUAL = "return float(a == b);";
- var NOT_EQUAL = "return float(a != b);";
- var LESS = "return float(a < b);";
- var LESS_EQUAL = "return float(a <= b);";
- var GREATER = "return float(a > b);";
- var GREATER_EQUAL = "return float(a >= b);";
- var LOGICAL_AND = "return float(a >= 1.0 && b >= 1.0);";
- var LOGICAL_OR = "return float(a >= 1.0 || b >= 1.0);";
- var MAX = CHECK_NAN_SNIPPET + "\n return max(a, b);\n";
- var MIN = CHECK_NAN_SNIPPET + "\n return min(a, b);\n";
- var MOD = "return mod(a, b);";
- var ATAN2 = CHECK_NAN_SNIPPET + "\n return atan(a, b);\n";
- var ELU_DER = "return (b >= 1.0) ? a : a * (b + 1.0);";
- var BinaryOpProgram = (function () {
- function BinaryOpProgram(op, aShape, bShape) {
- this.variableNames = ['A', 'B'];
- this.supportsBroadcasting = true;
- this.outputShape =
- assertAndGetBroadcastShape(aShape, bShape);
- this.userCode = "\n float binaryOperation(float a, float b) {\n " + op + "\n }\n\n void main() {\n float a = getAAtOutCoords();\n float b = getBAtOutCoords();\n setOutput(binaryOperation(a, b));\n }\n ";
- }
- return BinaryOpProgram;
- }());
-
- var ClipProgram = (function () {
- function ClipProgram(aShape, min, max) {
- this.variableNames = ['A'];
- this.outputShape = aShape;
- var minFixed = min.toFixed(20);
- var maxFixed = max.toFixed(20);
- this.userCode = "\n void main() {\n float value = getAAtOutCoords();\n if (isNaN(value)) {\n setOutput(value);\n return;\n }\n\n setOutput(clamp(value, " + minFixed + ", " + maxFixed + "));\n }\n ";
- }
- return ClipProgram;
- }());
-
- var ConcatProgram = (function () {
- function ConcatProgram(aShape, bShape) {
- this.variableNames = ['A', 'B'];
- this.outputShape = [];
- this.outputShape =
- computeOutShape(aShape, bShape, 1);
- this.userCode = "\n void main() {\n ivec2 coords = getOutputCoords();\n int yR = coords.x;\n int yC = coords.y;\n\n float value = 0.0;\n if (yC < " + aShape[1] + ") {\n value = getA(yR, yC);\n } else {\n yC -= " + aShape[1] + ";\n value = getB(yR, yC);\n }\n\n setOutput(value);\n }\n ";
- }
- return ConcatProgram;
- }());
-
- var Conv2DDerFilterProgram = (function () {
- function Conv2DDerFilterProgram(convInfo) {
- this.variableNames = ['x', 'dy'];
- this.outputShape = convInfo.filterShape;
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var padTop = convInfo.padInfo.top;
- var padLeft = convInfo.padInfo.left;
- 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 < " + convInfo.batchSize + "; b++) {\n for (int yR = 0; yR < " + convInfo.outHeight + "; yR++) {\n int xR = wR + yR * " + strideHeight + " - " + padTop + ";\n\n if (xR < 0 || xR >= " + convInfo.inHeight + ") {\n continue;\n }\n\n for (int yC = 0; yC < " + convInfo.outWidth + "; yC++) {\n int xC = wC + yC * " + strideWidth + " - " + padLeft + ";\n\n if (xC < 0 || xC >= " + convInfo.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 ";
- }
- return Conv2DDerFilterProgram;
- }());
- var Conv2DDerInputProgram = (function () {
- function Conv2DDerInputProgram(convInfo) {
- this.variableNames = ['dy', 'W'];
- this.outputShape = convInfo.inShape;
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var padTop = filterHeight - 1 - convInfo.padInfo.top;
- var padLeft = filterWidth - 1 - convInfo.padInfo.left;
- this.userCode = "\n const ivec2 pads = ivec2(" + padTop + ", " + padLeft + ");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords[3];\n\n ivec2 dyCorner = coords.yz - 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 < " + filterHeight + "; wR++) {\n float dyR = float(dyRCorner + wR) / " + strideHeight + ".0;\n\n if (dyR < 0.0 || dyR >= " + convInfo.outHeight + ".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = " + filterHeight + " - 1 - wR;\n\n for (int wC = 0; wC < " + filterWidth + "; wC++) {\n float dyC = float(dyCCorner + wC) / " + strideWidth + ".0;\n\n if (dyC < 0.0 || dyC >= " + convInfo.outWidth + ".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = " + filterWidth + " - 1 - wC;\n\n for (int d2 = 0; d2 < " + convInfo.outChannels + "; d2++) {\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n ";
- }
- return Conv2DDerInputProgram;
- }());
-
- var Conv2DProgram = (function () {
- function Conv2DProgram(convInfo) {
- this.variableNames = ['x', 'W'];
- this.outputShape = convInfo.outShape;
- var padTop = convInfo.padInfo.top;
- var padLeft = convInfo.padInfo.left;
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var dilationHeight = convInfo.dilationHeight;
- var dilationWidth = convInfo.dilationWidth;
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var inputDepthNearestVec4 = Math.floor(convInfo.inChannels / 4) * 4;
- var inputDepthVec4Remainder = convInfo.inChannels % 4;
- this.userCode = "\n const ivec2 strides = ivec2(" + strideHeight + ", " + strideWidth + ");\n const ivec2 pads = ivec2(" + padTop + ", " + padLeft + ");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d2 = coords[3];\n\n ivec2 xRCCorner = coords.yz * 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 < " + filterHeight + "; wR++) {\n int xR = xRCorner + wR * " + dilationHeight + ";\n\n if (xR < 0 || xR >= " + convInfo.inHeight + ") {\n continue;\n }\n\n for (int wC = 0; wC < " + filterWidth + "; wC++) {\n int xC = xCCorner + wC * " + dilationWidth + ";\n\n if (xC < 0 || xC >= " + convInfo.inWidth + ") {\n continue;\n }\n\n for (int d1 = 0; d1 < " + inputDepthNearestVec4 + "; d1 += 4) {\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 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 dotProd += dot(xValues, wValues);\n }\n\n if (" + (inputDepthVec4Remainder === 1) + ") {\n dotProd +=\n getX(batch, xR, xC, " + inputDepthNearestVec4 + ") *\n getW(wR, wC, " + inputDepthNearestVec4 + ", d2);\n } else if (" + (inputDepthVec4Remainder === 2) + ") {\n vec2 xValues = vec2(\n getX(batch, xR, xC, " + inputDepthNearestVec4 + "),\n getX(batch, xR, xC, " + inputDepthNearestVec4 + " + 1)\n );\n vec2 wValues = vec2(\n getW(wR, wC, " + inputDepthNearestVec4 + ", d2),\n getW(wR, wC, " + inputDepthNearestVec4 + " + 1, d2)\n );\n dotProd += dot(xValues, wValues);\n } else if (" + (inputDepthVec4Remainder === 3) + ") {\n vec3 xValues = vec3(\n getX(batch, xR, xC, " + inputDepthNearestVec4 + "),\n getX(batch, xR, xC, " + inputDepthNearestVec4 + " + 1),\n getX(batch, xR, xC, " + inputDepthNearestVec4 + " + 2)\n );\n vec3 wValues = vec3(\n getW(wR, wC, " + inputDepthNearestVec4 + ", d2),\n getW(wR, wC, " + inputDepthNearestVec4 + " + 1, d2),\n getW(wR, wC, " + inputDepthNearestVec4 + " + 2, d2)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n }\n setOutput(dotProd);\n }\n ";
- }
- return Conv2DProgram;
- }());
-
- var TextureType;
- (function (TextureType) {
- TextureType[TextureType["FLOAT"] = 0] = "FLOAT";
- TextureType[TextureType["UNSIGNED_BYTE"] = 1] = "UNSIGNED_BYTE";
- })(TextureType || (TextureType = {}));
- function getUnpackedMatrixTextureShapeWidthHeight(rows, columns) {
- return [columns, rows];
- }
- function getUnpackedArraySizeFromMatrixSize(matrixSize, channelsPerTexture) {
- return matrixSize * channelsPerTexture;
- }
- function getColorMatrixTextureShapeWidthHeight(rows, columns) {
- return [columns * 4, rows];
- }
- function getMatrixSizeFromUnpackedArraySize(unpackedSize, channelsPerTexture) {
- if (unpackedSize % channelsPerTexture !== 0) {
- throw new Error("unpackedSize (" + unpackedSize + ") must be a multiple of " +
- ("" + channelsPerTexture));
- }
- return unpackedSize / channelsPerTexture;
- }
- function encodeMatrixToUnpackedArray(matrix, unpackedArray, channelsPerTexture) {
- var requiredSize = getUnpackedArraySizeFromMatrixSize(matrix.length, channelsPerTexture);
- if (unpackedArray.length < requiredSize) {
- throw new Error("unpackedArray length (" + unpackedArray.length + ") must be >= " +
- ("" + requiredSize));
- }
- var dst = 0;
- for (var src = 0; src < matrix.length; ++src) {
- unpackedArray[dst] = matrix[src];
- dst += channelsPerTexture;
- }
- }
- var FLOAT_MAX = 20000;
- var FLOAT_MIN = -FLOAT_MAX;
- var FLOAT_RANGE = (FLOAT_MAX - FLOAT_MIN) / 255;
- var FLOAT_DELTAS = [1, 1 / 255, 1 / (255 * 255), 1 / (255 * 255 * 255)];
- var FLOAT_POWERS = [1, 255, 255 * 255];
- var BYTE_NAN_VALUE = 0;
- function encodeFloatArray(floatArray) {
- var uintArray = new Uint8Array(floatArray.length * 4);
- var _loop_1 = function (i) {
- var value = floatArray[i / 4];
- if (isNaN(value)) {
- uintArray[i] = BYTE_NAN_VALUE;
- uintArray[i + 1] = BYTE_NAN_VALUE;
- uintArray[i + 2] = BYTE_NAN_VALUE;
- uintArray[i + 3] = BYTE_NAN_VALUE;
- return "continue";
- }
- var normalizedValue = (value - FLOAT_MIN) / FLOAT_RANGE;
- var enc = FLOAT_POWERS.map(function (pow) { return pow * normalizedValue; });
- var buckets = enc.map(function (value) { return Math.floor((value % 1) * 255); });
- uintArray[i] = Math.floor(normalizedValue);
- uintArray[i + 1] = buckets[0];
- uintArray[i + 2] = buckets[1];
- uintArray[i + 3] = buckets[2];
- };
- for (var i = 0; i < uintArray.length; i += 4) {
- _loop_1(i);
- }
- return uintArray;
- }
- function decodeToFloatArray(uintArray) {
- var floatArray = new Float32Array(uintArray.length / 4);
- var _loop_2 = function (i) {
- if (uintArray[i] === BYTE_NAN_VALUE &&
- uintArray[i + 1] === BYTE_NAN_VALUE &&
- uintArray[i + 2] === BYTE_NAN_VALUE &&
- uintArray[i + 3] === BYTE_NAN_VALUE) {
- floatArray[i / 4] = NaN;
- return "continue";
- }
- var dot = 0;
- FLOAT_DELTAS.forEach(function (delta, j) {
- dot += delta * uintArray[i + j];
- });
- var value = dot * FLOAT_RANGE + FLOAT_MIN;
- floatArray[i / 4] = value;
- };
- for (var i = 0; i < uintArray.length; i += 4) {
- _loop_2(i);
- }
- return floatArray;
- }
- function decodeMatrixFromUnpackedArray(unpackedArray, matrix, channelsPerTexture) {
- var requiredSize = getMatrixSizeFromUnpackedArraySize(unpackedArray.length, channelsPerTexture);
- if (matrix.length < requiredSize) {
- throw new Error("matrix length (" + matrix.length + ") must be >= " + requiredSize);
- }
- var dst = 0;
- for (var src = 0; src < unpackedArray.length; src += channelsPerTexture) {
- matrix[dst++] = unpackedArray[src];
- }
- }
- function decodeMatrixFromUnpackedColorRGBAArray(unpackedArray, matrix, channels) {
- var requiredSize = unpackedArray.length * channels / 4;
- if (matrix.length < requiredSize) {
- throw new Error("matrix length (" + matrix.length + ") must be >= " + requiredSize);
- }
- var dst = 0;
- for (var src = 0; src < unpackedArray.length; src += 4) {
- for (var c = 0; c < channels; c++) {
- matrix[dst++] = unpackedArray[src + c];
- }
- }
- }
- function getPackedMatrixTextureShapeWidthHeight(rows, columns) {
- return [Math.ceil(columns / 2), Math.ceil(rows / 2)];
- }
- function getPackedRGBAArraySizeFromMatrixShape(rows, columns) {
- var _a = getPackedMatrixTextureShapeWidthHeight(rows, columns), w = _a[0], h = _a[1];
- return w * h * 4;
- }
- function encodeMatrixToPackedRGBA(matrix, rows, columns, packedRGBA) {
- var requiredSize = getPackedRGBAArraySizeFromMatrixShape(rows, columns);
- if (packedRGBA.length < requiredSize) {
- throw new Error("packedRGBA length (" + packedRGBA.length + ") must be >= " + requiredSize);
- }
- var _a = getPackedMatrixTextureShapeWidthHeight(rows, columns), textureWidth = _a[0], textureHeight = _a[1];
- var oddWidth = (columns % 2) === 1;
- var oddHeight = (rows % 2) === 1;
- var widthInFullBlocks = Math.floor(columns / 2);
- var heightInFullBlocks = Math.floor(rows / 2);
- {
- var dstStride = (oddWidth ? 4 : 0);
- var oneRow = columns;
- var dst = 0;
- for (var blockY = 0; blockY < heightInFullBlocks; ++blockY) {
- var matrixSrcRow = (blockY * 2 * columns);
- for (var blockX = 0; blockX < widthInFullBlocks; ++blockX) {
- var matrixSrcCol = blockX * 2;
- var src = matrixSrcRow + matrixSrcCol;
- packedRGBA[dst] = matrix[src];
- packedRGBA[dst + 1] = matrix[src + 1];
- packedRGBA[dst + 2] = matrix[src + oneRow];
- packedRGBA[dst + 3] = matrix[src + oneRow + 1];
- dst += 4;
- }
- dst += dstStride;
- }
- }
- if (oddWidth) {
- var src = columns - 1;
- var dst = (textureWidth - 1) * 4;
- var srcStride = 2 * columns;
- var dstStride = textureWidth * 4;
- for (var blockY = 0; blockY < heightInFullBlocks; ++blockY) {
- packedRGBA[dst] = matrix[src];
- packedRGBA[dst + 2] = matrix[src + columns];
- src += srcStride;
- dst += dstStride;
- }
- }
- if (oddHeight) {
- var src = (rows - 1) * columns;
- var dst = (textureHeight - 1) * textureWidth * 4;
- for (var blockX = 0; blockX < widthInFullBlocks; ++blockX) {
- packedRGBA[dst++] = matrix[src++];
- packedRGBA[dst++] = matrix[src++];
- dst += 2;
- }
- }
- if (oddWidth && oddHeight) {
- packedRGBA[packedRGBA.length - 4] = matrix[matrix.length - 1];
- }
- return packedRGBA;
- }
- function decodeMatrixFromPackedRGBA(packedRGBA, rows, columns, matrix) {
- var requiredSize = rows * columns;
- if (requiredSize < matrix.length) {
- throw new Error("matrix length (" + matrix.length + ") must be >= " + requiredSize);
- }
- var oddWidth = (columns % 2) === 1;
- var oddHeight = (rows % 2) === 1;
- var widthInFullBlocks = Math.floor(columns / 2);
- var heightInFullBlocks = Math.floor(rows / 2);
- var _a = getPackedMatrixTextureShapeWidthHeight(rows, columns), textureWidth = _a[0], textureHeight = _a[1];
- {
- var srcStride = oddWidth ? 4 : 0;
- var dstStride = columns + (oddWidth ? 1 : 0);
- var src = 0;
- var dstRow1 = 0;
- var dstRow2 = columns;
- for (var blockY = 0; blockY < heightInFullBlocks; ++blockY) {
- for (var blockX = 0; blockX < widthInFullBlocks; ++blockX) {
- matrix[dstRow1++] = packedRGBA[src++];
- matrix[dstRow1++] = packedRGBA[src++];
- matrix[dstRow2++] = packedRGBA[src++];
- matrix[dstRow2++] = packedRGBA[src++];
- }
- src += srcStride;
- dstRow1 += dstStride;
- dstRow2 += dstStride;
- }
- }
- if (oddWidth) {
- var src = (textureWidth - 1) * 4;
- var dst = columns - 1;
- var srcStride = textureWidth * 4;
- var dstStride = 2 * columns;
- for (var blockY = 0; blockY < heightInFullBlocks; ++blockY) {
- matrix[dst] = packedRGBA[src];
- matrix[dst + columns] = packedRGBA[src + 2];
- src += srcStride;
- dst += dstStride;
- }
- }
- if (oddHeight) {
- var src = (textureHeight - 1) * textureWidth * 4;
- var dst = (rows - 1) * columns;
- for (var blockX = 0; blockX < widthInFullBlocks; ++blockX) {
- matrix[dst++] = packedRGBA[src++];
- matrix[dst++] = packedRGBA[src++];
- src += 2;
- }
- }
- if (oddWidth && oddHeight) {
- matrix[matrix.length - 1] = packedRGBA[packedRGBA.length - 4];
- }
- return matrix;
- }
-
- function makeShader(inputsInfo, outputShape, userCode, broadcast) {
- var sampleSnippet = getSampleSnippet();
- var setOutputSnippet = getSetOutputSnippet();
- var inputPrefixSnippet = inputsInfo.map(function (x) { return "uniform sampler2D " + x.name + ";"; }).join('\n');
- var inputSamplingSnippet = inputsInfo.map(function (x) { return getInputSamplingSnippet(x, outputShape, broadcast); })
- .join('\n');
- var outTexShape = outputShape.texShape;
- var outputSamplingSnippet = getOutputSamplingSnippet(outputShape.logicalShape, outTexShape);
- var source = [
- SHADER_PREFIX, sampleSnippet, setOutputSnippet, inputPrefixSnippet,
- outputSamplingSnippet, inputSamplingSnippet, userCode
- ].join('\n');
- return source;
- }
- function getSampleSnippet() {
- return ENV.get('WEBGL_FLOAT_TEXTURE_ENABLED') ?
- FLOAT_TEXTURE_SAMPLE_SNIPPET :
- UNSIGNED_BYTE_TEXTURE_SAMPLE_SNIPPET;
- }
- function getSetOutputSnippet() {
- return ENV.get('WEBGL_FLOAT_TEXTURE_ENABLED') ?
- FLOAT_TEXTURE_SETOUTPUT_SNIPPET :
- UNSIGNED_BYTE_TEXTURE_SETOUTPUT_SNIPPET;
- }
- function getSamplerFromInInfo(inInfo) {
- var shape = inInfo.shapeInfo.logicalShape;
- switch (shape.length) {
- case 0:
- return getSamplerScalar(inInfo);
- case 1:
- return getSampler1D(inInfo);
- case 2:
- return getSampler2D(inInfo);
- case 3:
- return getSampler3D(inInfo);
- case 4:
- return getSampler4D(inInfo);
- default:
- throw new Error(shape.length + "-D input sampling" +
- " is not yet supported");
- }
- }
- function getInputSamplingSnippet(inInfo, outShapeInfo, broadcast) {
- var res = getSamplerFlat(inInfo);
- res += getSamplerFromInInfo(inInfo);
- if (broadcast ||
- arraysEqual(inInfo.shapeInfo.logicalShape, outShapeInfo.logicalShape)) {
- res += getSamplerAtOutputCoords(inInfo, outShapeInfo, broadcast);
- }
- return res;
- }
- function getOutputSamplingSnippet(outShape, outTexShape) {
- switch (outShape.length) {
- case 0:
- return getOutputScalarCoords();
- case 1:
- return getOutput1DCoords(outShape, outTexShape);
- case 2:
- return getOutput2DCoords(outShape, outTexShape);
- case 3:
- return getOutput3DCoords(outShape, outTexShape);
- case 4:
- return getOutput4DCoords(outShape, outTexShape);
- default:
- throw new Error(outShape.length + "-D output sampling is not yet supported");
- }
- }
- var SAMPLE_1D_SNIPPET = "\nvec2 UVfrom1D(int texNumR, int texNumC, int index) {\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n";
- var SAMPLE_2D_SNIPPET = "\nvec2 UVfrom2D(int texNumR, int texNumC, int numC, int row, int col) {\n int index = row * numC + col;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n";
- var SAMPLE_3D_SNIPPET = "\nvec2 UVfrom3D(int texNumR, int texNumC, int stride0,\n int stride1, int row, int col, int depth) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n";
- var SAMPLE_4D_SNIPPET = "\nvec2 UVfrom4D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int row, int col, int depth,\n int depth2) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth * stride2 + depth2;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n";
- var UNSIGNED_BYTE_TEXTURE_SAMPLE_SNIPPET = "\n uniform float NaN;\n\n const vec4 floatDeltas = vec4(\n 1.0,\n 1.0 / 255.0,\n 1.0 / (255.0 * 255.0),\n 1.0 / (255.0 * 255.0 * 255.0)\n );\n const float minValue = " + FLOAT_MIN + ".0;\n const float maxValue = " + FLOAT_MAX + ".0;\n const float range = (maxValue - minValue) / 255.0;\n const vec2 dotRange = vec2(1.0, range);\n\n float sampleTexture(sampler2D textureSampler, vec2 uv) {\n vec4 sampleValue = texture2D(textureSampler, uv);\n if (all(equal(sampleValue, vec4(" + BYTE_NAN_VALUE + ")))) {\n return NaN;\n }\n\n vec4 encValue = floor(sampleValue * 255.0 + 0.5);\n float decodedValue = dot(encValue, floatDeltas);\n return dot(vec2(minValue, decodedValue), dotRange);\n }\n";
- var UNSIGNED_BYTE_TEXTURE_SETOUTPUT_SNIPPET = "\n const vec4 floatPowers = vec4(\n 1.0,\n 255.0,\n 255.0 * 255.0,\n 255.0 * 255.0 * 255.0\n );\n const vec2 recipRange = vec2(1.0/range);\n const vec2 recipRange255 = vec2(1.0/(maxValue - minValue));\n\n void setOutput(float decodedValue) {\n if (isNaN(decodedValue)) {\n gl_FragColor = vec4(" + BYTE_NAN_VALUE + ");\n return;\n }\n\n float a = dot(vec2(decodedValue, -minValue), recipRange);\n float b = fract(a) * 255.0;\n float c = fract(b) * 255.0;\n float d = fract(c) * 255.0;\n gl_FragColor = floor(vec4(a, b, c, d)) / 255.0;\n\n // TODO(dsmilkov): Version above gets better accuracy but probably slower\n // than the version below. Benchmark to determine if the accuracy is worth\n // the cost.\n\n // float normValue = dot(vec2(decodedValue, -minValue), recipRange255);\n // vec4 f = normValue * floatPowers;\n // gl_FragColor = floor(fract(f) * 255.0) / 255.0;\n }\n";
- var FLOAT_TEXTURE_SAMPLE_SNIPPET = "\n float sampleTexture(sampler2D textureSampler, vec2 uv) {\n return texture2D(textureSampler, uv).r;\n }\n";
- var FLOAT_TEXTURE_SETOUTPUT_SNIPPET = "\n void setOutput(float val) {\n gl_FragColor = vec4(val, 0, 0, 0);\n }\n";
- var SHADER_PREFIX = "\n precision highp float;\n precision highp int;\n varying vec2 resultUV;\n const vec2 halfCR = vec2(0.5, 0.5);\n\n bool isNaN(float val) {\n float v1 = val * val;\n float v2 = val * val;\n return v1 == v2 ? false : true;\n }\n\n bool hasNaN(vec4 values) {\n vec4 v1 = values * values;\n vec4 v2 = values * values;\n return any(notEqual(v1, v2));\n }\n\n float getNaN(vec4 values) {\n return dot(vec4(1), values);\n }\n\n int round(float value) {\n return int(floor(value + 0.5));\n }\n\n int imod(int x, int y) {\n return x - y * (x / y);\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 " + SAMPLE_1D_SNIPPET + "\n " + SAMPLE_2D_SNIPPET + "\n " + SAMPLE_3D_SNIPPET + "\n " + SAMPLE_4D_SNIPPET + "\n";
- function getOutputScalarCoords() {
- return "\n int getOutputCoords() {\n return 0;\n }\n ";
- }
- function getOutput1DCoords(shape, texShape) {
- if (texShape[0] === 1) {
- return "\n int getOutputCoords() {\n return int(resultUV.x * " + texShape[1] + ".0);\n }\n ";
- }
- if (texShape[1] === 1) {
- return "\n int getOutputCoords() {\n return int(resultUV.y * " + texShape[0] + ".0);\n }\n ";
- }
- return "\n int getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2(" + texShape[0] + ", " + texShape[1] + "));\n return resTexRC.x * " + texShape[1] + " + resTexRC.y;\n }\n ";
- }
- function getOutput3DCoords(shape, texShape) {
- var stride0 = shape[1] * shape[2];
- var stride1 = shape[2];
- return "\n ivec3 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2(" + texShape[0] + ", " + texShape[1] + "));\n int index = resTexRC.x * " + texShape[1] + " + resTexRC.y;\n int r = index / " + stride0 + ";\n index -= r * " + stride0 + ";\n int c = index / " + stride1 + ";\n int d = index - c * " + stride1 + ";\n return ivec3(r, c, d);\n }\n ";
- }
- function getOutput4DCoords(shape, texShape) {
- var stride2 = shape[3];
- var stride1 = shape[2] * stride2;
- var stride0 = shape[1] * stride1;
- return "\n ivec4 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2(" + texShape[0] + ", " + texShape[1] + "));\n int index = resTexRC.x * " + texShape[1] + " + resTexRC.y;\n\n int r = index / " + stride0 + ";\n index -= r * " + stride0 + ";\n\n int c = index / " + stride1 + ";\n index -= c * " + stride1 + ";\n\n int d = index / " + stride2 + ";\n int d2 = index - d * " + stride2 + ";\n\n return ivec4(r, c, d, d2);\n }\n ";
- }
- function getOutput2DCoords(shape, texShape) {
- if (arraysEqual(shape, texShape)) {
- return "\n ivec2 getOutputCoords() {\n return ivec2(resultUV.yx * vec2(" + texShape[0] + ", " + texShape[1] + "));\n }\n ";
- }
- if (shape[1] === 1) {
- return "\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2(" + texShape[0] + ", " + texShape[1] + "));\n int index = resTexRC.x * " + texShape[1] + " + resTexRC.y;\n return ivec2(index, 0);\n }\n ";
- }
- if (shape[0] === 1) {
- return "\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2(" + texShape[0] + ", " + texShape[1] + "));\n int index = resTexRC.x * " + texShape[1] + " + resTexRC.y;\n return ivec2(0, index);\n }\n ";
- }
- return "\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2(" + texShape[0] + ", " + texShape[1] + "));\n int index = resTexRC.x * " + texShape[1] + " + resTexRC.y;\n int r = index / " + shape[1] + ";\n int c = index - r * " + shape[1] + ";\n return ivec2(r, c);\n }\n ";
- }
- function getSamplerScalar(inputInfo) {
- var texName = inputInfo.name;
- var funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1);
- return "\n float " + funcName + "() {\n return sampleTexture(" + texName + ", halfCR);\n }\n ";
- }
- function getSampler1D(inputInfo) {
- var texName = inputInfo.name;
- var funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1);
- return "\n float " + funcName + "(int index) {\n return " + funcName + "Flat(index);\n }\n ";
- }
- function getSampler2D(inputInfo) {
- var shape = inputInfo.shapeInfo.logicalShape;
- var texShape = inputInfo.shapeInfo.texShape;
- var texName = inputInfo.name;
- var funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1);
- var texNumR = texShape[0];
- var texNumC = texShape[1];
- if (arraysEqual(shape, texShape)) {
- return "\n float " + funcName + "(int row, int col) {\n vec2 uv = (vec2(col, row) + halfCR) / vec2(" + texNumC + ".0, " + texNumR + ".0);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- var _a = squeezeShape(shape), newShape = _a.newShape, keptDims = _a.keptDims;
- var squeezedShape = newShape;
- if (squeezedShape.length < shape.length) {
- var newInputInfo = squeezeInputInfo(inputInfo, squeezedShape);
- var params = ['row', 'col'];
- return "\n " + getSamplerFromInInfo(newInputInfo) + "\n float " + funcName + "(int row, int col) {\n return " + funcName + "(" + getSqueezedParams(params, keptDims) + ");\n }\n ";
- }
- if (texNumC === 1) {
- return "\n float " + funcName + "(int row, int col) {\n int index = row * " + shape[1] + " + col;\n vec2 uv = vec2(0.5, (float(index) + 0.5) / " + texNumR + ".0);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- if (texNumR === 1) {
- return "\n float " + funcName + "(int row, int col) {\n int index = row * " + shape[1] + " + col;\n vec2 uv = vec2((float(index) + 0.5) / " + texNumC + ".0, 0.5);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- return "\n float " + funcName + "(int row, int col) {\n vec2 uv = UVfrom2D(" + texNumR + ", " + texNumC + ", " + shape[1] + ", row, col);\n return sampleTexture(" + texName + ", uv);\n }\n";
- }
- function getSampler3D(inputInfo) {
- var texShape = inputInfo.shapeInfo.texShape;
- var shape = inputInfo.shapeInfo.logicalShape;
- var texName = inputInfo.name;
- var funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1);
- var texNumR = texShape[0];
- var texNumC = texShape[1];
- var stride0 = shape[1] * shape[2];
- var stride1 = shape[2];
- var _a = squeezeShape(shape), newShape = _a.newShape, keptDims = _a.keptDims;
- var squeezedShape = newShape;
- if (squeezedShape.length < shape.length) {
- var newInputInfo = squeezeInputInfo(inputInfo, squeezedShape);
- var params = ['row', 'col', 'depth'];
- return "\n " + getSamplerFromInInfo(newInputInfo) + "\n float " + funcName + "(int row, int col, int depth) {\n return " + funcName + "(" + getSqueezedParams(params, keptDims) + ");\n }\n ";
- }
- if (texNumC === stride0) {
- return "\n float " + funcName + "(int row, int col, int depth) {\n int texR = row;\n int texC = col * " + stride1 + " + depth;\n vec2 uv = (vec2(texC, texR) + halfCR) /\n vec2(" + texNumC + ".0, " + texNumR + ".0);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- if (texNumC === stride1) {
- return "\n float " + funcName + "(int row, int col, int depth) {\n int texR = row * " + shape[1] + " + col;\n int texC = depth;\n vec2 uv = (vec2(texC, texR) + halfCR) / vec2(" + texNumC + ".0, " + texNumR + ".0);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- return "\n float " + funcName + "(int row, int col, int depth) {\n vec2 uv = UVfrom3D(\n " + texNumR + ", " + texNumC + ", " + stride0 + ", " + stride1 + ", row, col, depth);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- function getSampler4D(inputInfo) {
- var shape = inputInfo.shapeInfo.logicalShape;
- var texShape = inputInfo.shapeInfo.texShape;
- var texName = inputInfo.name;
- var funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1);
- var texNumR = texShape[0];
- var texNumC = texShape[1];
- var stride2 = shape[3];
- var stride1 = shape[2] * stride2;
- var stride0 = shape[1] * stride1;
- var _a = squeezeShape(shape), newShape = _a.newShape, keptDims = _a.keptDims;
- if (newShape.length < shape.length) {
- var newInputInfo = squeezeInputInfo(inputInfo, newShape);
- var params = ['row', 'col', 'depth', 'depth2'];
- return "\n " + getSamplerFromInInfo(newInputInfo) + "\n float " + funcName + "(int row, int col, int depth, int depth2) {\n return " + funcName + "(" + getSqueezedParams(params, keptDims) + ");\n }\n ";
- }
- if (texNumC === stride0) {
- return "\n float " + funcName + "(int row, int col, int depth, int depth2) {\n int texR = row;\n int texC = col * " + stride1 + " + depth * " + stride2 + " + depth2;\n vec2 uv = (vec2(texC, texR) + halfCR) /\n vec2(" + texNumC + ".0, " + texNumR + ".0);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- if (texNumC === stride2) {
- return "\n float " + funcName + "(int row, int col, int depth, int depth2) {\n int texR = row * " + shape[1] * shape[2] + " + col * " + shape[2] + " + depth;\n int texC = depth2;\n vec2 uv = (vec2(texC, texR) + halfCR) /\n vec2(" + texNumC + ".0, " + texNumR + ".0);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- return "\n float " + funcName + "(int row, int col, int depth, int depth2) {\n vec2 uv = UVfrom4D(" + texNumR + ", " + texNumC + ", " + stride0 + ", " + stride1 + ",\n " + stride2 + ", row, col, depth, depth2);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- function getSamplerFlat(inputInfo) {
- var texName = inputInfo.name;
- var texShape = inputInfo.shapeInfo.texShape;
- var funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1) + 'Flat';
- var tNumR = texShape[0];
- var tNumC = texShape[1];
- if (tNumC === 1 && tNumR === 1) {
- return "\n float " + funcName + "(int index) {\n return sampleTexture(" + texName + ", halfCR);\n }\n ";
- }
- if (tNumC === 1) {
- return "\n float " + funcName + "(int index) {\n vec2 uv = vec2(0.5, (float(index) + 0.5) / " + tNumR + ".0);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- if (tNumR === 1) {
- return "\n float " + funcName + "(int index) {\n vec2 uv = vec2((float(index) + 0.5) / " + tNumC + ".0, 0.5);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- return "\n float " + funcName + "(int index) {\n vec2 uv = UVfrom1D(" + tNumR + ", " + tNumC + ", index);\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- function getBroadcastOutputCoordsSampler(inputInfo, outShapeInfo, texFuncSnippet, funcName) {
- var inRank = inputInfo.shapeInfo.logicalShape.length;
- var outRank = outShapeInfo.logicalShape.length;
- var type = 'int';
- if (outRank === 2) {
- type = 'ivec2';
- }
- else if (outRank === 3) {
- type = 'ivec3';
- }
- else if (outRank === 4) {
- type = 'ivec4';
- }
- var broadcastDims = getBroadcastDims(inputInfo.shapeInfo.logicalShape, outShapeInfo.logicalShape);
- var rankDiff = outRank - inRank;
- var coordsSnippet;
- if (inRank === 0) {
- coordsSnippet = '';
- }
- else if (outRank < 2 && broadcastDims.length >= 1) {
- coordsSnippet = 'coords = 0;';
- }
- else {
- coordsSnippet =
- broadcastDims.map(function (d) { return "coords[" + (d + rankDiff) + "] = 0;"; }).join('\n');
- }
- var unpackedCoordsSnippet = '';
- if (outRank < 2 && inRank > 0) {
- unpackedCoordsSnippet = 'coords';
- }
- else {
- unpackedCoordsSnippet = inputInfo.shapeInfo.logicalShape
- .map(function (s, i) { return "coords[" + (i + rankDiff) + "]"; })
- .join(', ');
- }
- return "\n float " + funcName + "() {\n " + type + " coords = getOutputCoords();\n " + coordsSnippet + "\n return get" + texFuncSnippet + "(" + unpackedCoordsSnippet + ");\n }\n ";
- }
- function getSamplerAtOutputCoords(inputInfo, outShapeInfo, supportsBroadcasting) {
- var inTexShape = inputInfo.shapeInfo.texShape;
- var texName = inputInfo.name;
- var texFuncSnippet = texName.charAt(0).toUpperCase() + texName.slice(1);
- var funcName = 'get' + texFuncSnippet + 'AtOutCoords';
- var broadcastDims = getBroadcastDims(inputInfo.shapeInfo.logicalShape, outShapeInfo.logicalShape);
- var inRank = inputInfo.shapeInfo.logicalShape.length;
- var outRank = outShapeInfo.logicalShape.length;
- var doBroadcast = supportsBroadcasting && ((outRank > inRank) || broadcastDims.length > 0);
- var broadcastOverOuter = broadcastDimsAreOuter(broadcastDims);
- if (doBroadcast && !broadcastOverOuter) {
- return getBroadcastOutputCoordsSampler(inputInfo, outShapeInfo, texFuncSnippet, funcName);
- }
- var outTexShape = outShapeInfo.texShape;
- if (arraysEqual(inTexShape, outTexShape)) {
- return "\n float " + funcName + "() {\n return sampleTexture(" + texName + ", resultUV);\n }\n ";
- }
- var inSize = sizeFromShape(inTexShape);
- var broadcastSnippet = '';
- if (doBroadcast && broadcastOverOuter) {
- broadcastSnippet = "\n int mainPart = index / " + inSize + ";\n index -= mainPart * " + inSize + ";\n ";
- }
- return "\n float " + funcName + "() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2(" + outTexShape[0] + ", " + outTexShape[1] + "));\n int index = resTexRC.x * " + outTexShape[1] + " + resTexRC.y;\n " + broadcastSnippet + "\n int texR = index / " + inTexShape[1] + ";\n int texC = index - texR * " + inTexShape[1] + ";\n vec2 uv = (vec2(texC, texR) + halfCR) /\n vec2(" + inTexShape[1] + ".0, " + inTexShape[0] + ".0);\n\n return sampleTexture(" + texName + ", uv);\n }\n ";
- }
- function getCoordsDataType(rank) {
- if (rank <= 1) {
- return 'int';
- }
- else if (rank === 2) {
- return 'ivec2';
- }
- else if (rank === 3) {
- return 'ivec3';
- }
- else if (rank === 4) {
- return 'ivec4';
- }
- else {
- throw Error("GPU for rank " + rank + " is not yet supported");
- }
- }
- function squeezeInputInfo(inInfo, squeezedShape) {
- var newInputInfo = JSON.parse(JSON.stringify(inInfo));
- newInputInfo.shapeInfo.logicalShape = squeezedShape;
- return newInputInfo;
- }
- function getSqueezedParams(params, keptDims) {
- return keptDims.map(function (d) { return params[d]; }).join(', ');
- }
-
- var CumSumProgram = (function () {
- function CumSumProgram(shape, exclusive, reverse) {
- this.variableNames = ['x'];
- this.outputShape = shape;
- var rank = shape.length;
- var finalDim = shape[shape.length - 1];
- var dtype = getCoordsDataType(rank);
- var outputCoords = getCoords(rank, 'coords');
- var sourceCoords = getCoords(rank, 'adjustableCoords');
- var finalCoord = getFinalCoord(rank, 'coords');
- var finalAdjustableCoord = getFinalCoord(rank, 'adjustableCoords');
- var indexAdjuster = reverse ? "return " + finalDim + " -i - 1;" : 'return i;';
- var comparator = reverse ? '<' : '>';
- this.userCode = "\n int getIndex(int i) {\n " + indexAdjuster + "\n }\n\n void main() {\n " + dtype + " coords = getOutputCoords();\n " + dtype + " adjustableCoords = " + dtype + "(" + outputCoords + ");\n int finalCoord = int(" + finalCoord + ");\n float val = 0.0;\n for (int i = " + finalDim + " - 1; i >= 0; i -= 1) {\n int idx = getIndex(i);\n if (idx " + comparator + " finalCoord) {\n continue;\n }\n if (idx == finalCoord && " + exclusive + ") {\n continue;\n }\n " + finalAdjustableCoord + " = idx;\n val += getX(" + sourceCoords + ");\n }\n setOutput(val);\n }\n ";
- }
- return CumSumProgram;
- }());
- function getCoords(rank, name) {
- if (rank === 1) {
- return "" + name;
- }
- else if (rank === 2) {
- return name + ".x, " + name + ".y";
- }
- else if (rank === 3) {
- return name + ".x, " + name + ".y, " + name + ".z";
- }
- else if (rank === 4) {
- return name + ".x, " + name + ".y, " + name + ".z, " + name + ".w";
- }
- else {
- throw Error("Cumulative sum for rank " + rank + " is not yet supported");
- }
- }
- function getFinalCoord(rank, name) {
- if (rank === 1) {
- return "" + name;
- }
- else if (rank === 2) {
- return name + ".y";
- }
- else if (rank === 3) {
- return name + ".z";
- }
- else if (rank === 4) {
- return name + ".w";
- }
- else {
- throw Error("Cumulative sum for rank " + rank + " is not yet supported");
- }
- }
-
- var DepthwiseConv2DProgram = (function () {
- function DepthwiseConv2DProgram(convInfo) {
- this.variableNames = ['x', 'W'];
- this.outputShape = convInfo.outShape;
- var xNumRows = convInfo.inHeight;
- var xNumCols = convInfo.inWidth;
- var padTop = convInfo.padInfo.top;
- var padLeft = convInfo.padInfo.left;
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var dilationHeight = convInfo.dilationHeight;
- var dilationWidth = convInfo.dilationWidth;
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var channelMul = convInfo.outChannels / convInfo.inChannels;
- this.userCode = "\n const ivec2 strides = ivec2(" + strideHeight + ", " + strideWidth + ");\n const ivec2 pads = ivec2(" + padTop + ", " + padLeft + ");\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 / " + channelMul + ";\n int q = d2 - d1 * " + channelMul + ";\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 // TODO(dsmilkov): Flatten the two for loops and vec4 the operations.\n for (int wR = 0; wR < " + filterHeight + "; wR++) {\n int xR = xRCorner + wR * " + dilationHeight + ";\n\n if (xR < 0 || xR >= " + xNumRows + ") {\n continue;\n }\n\n for (int wC = 0; wC < " + filterWidth + "; wC++) {\n int xC = xCCorner + wC * " + dilationWidth + ";\n\n if (xC < 0 || xC >= " + xNumCols + ") {\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 setOutput(dotProd);\n }\n ";
- }
- return DepthwiseConv2DProgram;
- }());
-
- var FromPixelsProgram = (function () {
- function FromPixelsProgram(outputShape) {
- this.variableNames = ['A'];
- var height = outputShape[0], width = outputShape[1];
- this.outputShape = outputShape;
- this.userCode = "\n void main() {\n ivec3 coords = getOutputCoords();\n int texR = coords[0];\n int texC = coords[1];\n int depth = coords[2];\n vec2 uv = (vec2(texC, texR) + halfCR) / vec2(" + width + ".0, " + height + ".0);\n\n vec4 values = texture2D(A, uv);\n float value;\n if (depth == 0) {\n value = values.r;\n } else if (depth == 1) {\n value = values.g;\n } else if (depth == 2) {\n value = values.b;\n } else if (depth == 3) {\n value = values.a;\n }\n\n setOutput(floor(value * 255.0 + 0.5));\n }\n ";
- }
- return FromPixelsProgram;
- }());
-
- var GatherProgram = (function () {
- function GatherProgram(aShape, indicesLength, axis) {
- this.variableNames = ['A', 'indices'];
- var outputShape = aShape.slice();
- outputShape[axis] = indicesLength;
- this.outputShape = outputShape;
- this.rank = outputShape.length;
- var dtype = getCoordsDataType(this.rank);
- var sourceCoords = getSourceCoords(aShape, axis);
- this.userCode = "\n void main() {\n " + dtype + " resRC = getOutputCoords();\n setOutput(getA(" + sourceCoords + "));\n }\n ";
- }
- return GatherProgram;
- }());
- function getSourceCoords(aShape, axis) {
- var rank = aShape.length;
- if (rank > 4) {
- throw Error("Gather for rank " + rank + " is not yet supported");
- }
- if (rank === 1) {
- return "int(getIndices(resRC))";
- }
- var currentCoords = ['resRC.x', 'resRC.y', 'resRC.z', 'resRC.w'];
- var sourceCoords = [];
- for (var i = 0; i < aShape.length; i++) {
- if (i === axis) {
- sourceCoords.push("int(getIndices(" + currentCoords[i] + "))");
- }
- else {
- sourceCoords.push("" + currentCoords[i]);
- }
- }
- return sourceCoords.join();
- }
-
- var MAX_TEXTURE_SIZE = null;
- function createWebGLRenderingContext(attributes) {
- var canvas = document.createElement('canvas');
- canvas.width = 1;
- canvas.height = 1;
- return createWebGLRenderingContextFromCanvas(canvas, attributes);
- }
- function createWebGLRenderingContextFromCanvas(canvas, attributes) {
- var gl;
- var webglVersion = ENV.get('WEBGL_VERSION');
- if (webglVersion === 2) {
- gl = canvas.getContext('webgl2', attributes);
- }
- else if (webglVersion === 1) {
- gl = (canvas.getContext('webgl', attributes) ||
- canvas.getContext('experimental-webgl', attributes));
- }
- if (webglVersion === 0 || gl == null) {
- throw new Error('This browser does not support WebGL.');
- }
- return gl;
- }
- function callAndCheck(gl, func) {
- var returnValue = func();
- checkWebGLError(gl);
- return returnValue;
- }
- var webGLDebugErrorCheckingEnabled = false;
- function enableDebugWebGLErrorChecking(enabled) {
- webGLDebugErrorCheckingEnabled = enabled;
- }
- function checkWebGLError(gl) {
- if (webGLDebugErrorCheckingEnabled) {
- var error = gl.getError();
- if (error !== gl.NO_ERROR) {
- throw new Error('WebGL Error: ' + getWebGLErrorMessage(gl, error));
- }
- }
- }
- function getWebGLErrorMessage(gl, status) {
- switch (status) {
- case gl.NO_ERROR:
- return 'NO_ERROR';
- case gl.INVALID_ENUM:
- return 'INVALID_ENUM';
- case gl.INVALID_VALUE:
- return 'INVALID_VALUE';
- case gl.INVALID_OPERATION:
- return 'INVALID_OPERATION';
- case gl.INVALID_FRAMEBUFFER_OPERATION:
- return 'INVALID_FRAMEBUFFER_OPERATION';
- case gl.OUT_OF_MEMORY:
- return 'OUT_OF_MEMORY';
- case gl.CONTEXT_LOST_WEBGL:
- return 'CONTEXT_LOST_WEBGL';
- default:
- return "Unknown error code " + status;
- }
- }
- function getExtensionOrThrow(gl, extensionName) {
- return throwIfNull(gl, function () { return gl.getExtension(extensionName); }, 'Extension "' + extensionName + '" not supported on this browser.');
- }
- function createVertexShader(gl, vertexShaderSource) {
- var vertexShader = throwIfNull(gl, function () { return gl.createShader(gl.VERTEX_SHADER); }, 'Unable to create vertex WebGLShader.');
- callAndCheck(gl, function () { return gl.shaderSource(vertexShader, vertexShaderSource); });
- callAndCheck(gl, function () { return gl.compileShader(vertexShader); });
- if (gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS) === false) {
- console.log(gl.getShaderInfoLog(vertexShader));
- throw new Error('Failed to compile vertex shader.');
- }
- return vertexShader;
- }
- function createFragmentShader(gl, fragmentShaderSource) {
- var fragmentShader = throwIfNull(gl, function () { return gl.createShader(gl.FRAGMENT_SHADER); }, 'Unable to create fragment WebGLShader.');
- callAndCheck(gl, function () { return gl.shaderSource(fragmentShader, fragmentShaderSource); });
- callAndCheck(gl, function () { return gl.compileShader(fragmentShader); });
- if (gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS) === false) {
- logShaderSourceAndInfoLog(fragmentShaderSource, gl.getShaderInfoLog(fragmentShader));
- throw new Error('Failed to compile fragment shader.');
- }
- return fragmentShader;
- }
- var lineNumberRegex = /ERROR: [0-9]+:([0-9]+):/g;
- function logShaderSourceAndInfoLog(shaderSource, shaderInfoLog) {
- var lineNumberRegexResult = lineNumberRegex.exec(shaderInfoLog);
- if (lineNumberRegexResult == null) {
- console.log("Couldn't parse line number in error: " + shaderInfoLog);
- console.log(shaderSource);
- return;
- }
- var lineNumber = +lineNumberRegexResult[1];
- var shaderLines = shaderSource.split('\n');
- var pad = shaderLines.length.toString().length + 2;
- var linesWithLineNumbers = shaderLines.map(function (line, lineNumber) {
- return rightPad((lineNumber + 1).toString(), pad) + line;
- });
- var maxLineLength = 0;
- for (var i = 0; i < linesWithLineNumbers.length; i++) {
- maxLineLength = Math.max(linesWithLineNumbers[i].length, maxLineLength);
- }
- var beforeErrorLines = linesWithLineNumbers.slice(0, lineNumber - 1);
- var errorLine = linesWithLineNumbers.slice(lineNumber - 1, lineNumber);
- var afterErrorLines = linesWithLineNumbers.slice(lineNumber);
- console.log(beforeErrorLines.join('\n'));
- console.log(shaderInfoLog.split('\n')[0]);
- console.log("%c " + rightPad(errorLine[0], maxLineLength), 'border:1px solid red; background-color:#e3d2d2; color:#a61717');
- console.log(afterErrorLines.join('\n'));
- }
- function createProgram(gl) {
- return throwIfNull(gl, function () { return gl.createProgram(); }, 'Unable to create WebGLProgram.');
- }
- function linkProgram(gl, program) {
- callAndCheck(gl, function () { return gl.linkProgram(program); });
- if (gl.getProgramParameter(program, gl.LINK_STATUS) === false) {
- console.log(gl.getProgramInfoLog(program));
- throw new Error('Failed to link vertex and fragment shaders.');
- }
- }
- function validateProgram(gl, program) {
- callAndCheck(gl, function () { return gl.validateProgram(program); });
- if (gl.getProgramParameter(program, gl.VALIDATE_STATUS) === false) {
- console.log(gl.getProgramInfoLog(program));
- throw new Error('Shader program validation failed.');
- }
- }
- function createStaticVertexBuffer(gl, data) {
- var buffer = throwIfNull(gl, function () { return gl.createBuffer(); }, 'Unable to create WebGLBuffer');
- callAndCheck(gl, function () { return gl.bindBuffer(gl.ARRAY_BUFFER, buffer); });
- callAndCheck(gl, function () { return gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW); });
- return buffer;
- }
- function createStaticIndexBuffer(gl, data) {
- var buffer = throwIfNull(gl, function () { return gl.createBuffer(); }, 'Unable to create WebGLBuffer');
- callAndCheck(gl, function () { return gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer); });
- callAndCheck(gl, function () { return gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, gl.STATIC_DRAW); });
- return buffer;
- }
- function queryMaxTextureSize(gl) {
- if (MAX_TEXTURE_SIZE != null) {
- return MAX_TEXTURE_SIZE;
- }
- MAX_TEXTURE_SIZE =
- callAndCheck(gl, function () { return gl.getParameter(gl.MAX_TEXTURE_SIZE); });
- return MAX_TEXTURE_SIZE;
- }
- function getChannelsPerTexture() {
- if (!ENV.get('WEBGL_FLOAT_TEXTURE_ENABLED')) {
- return 4;
- }
- if (ENV.get('WEBGL_VERSION') === 2) {
- return 1;
- }
- return 4;
- }
- function createTexture(gl) {
- return throwIfNull(gl, function () { return gl.createTexture(); }, 'Unable to create WebGLTexture.');
- }
- function validateTextureSize(gl, width, height) {
- var maxTextureSize = queryMaxTextureSize(gl);
- if ((width <= 0) || (height <= 0)) {
- var requested = "[" + width + "x" + height + "]";
- throw new Error('Requested texture size ' + requested + ' is invalid.');
- }
- if ((width > maxTextureSize) || (height > maxTextureSize)) {
- var requested = "[" + width + "x" + height + "]";
- var max = "[" + maxTextureSize + "x" + maxTextureSize + "]";
- throw new Error('Requested texture size ' + requested +
- ' greater than WebGL maximum on this browser / GPU ' + max + '.');
- }
- }
- function createFramebuffer(gl) {
- return throwIfNull(gl, function () { return gl.createFramebuffer(); }, 'Unable to create WebGLFramebuffer.');
- }
- function bindVertexBufferToProgramAttribute(gl, program, attribute, buffer, arrayEntriesPerItem, itemStrideInBytes, itemOffsetInBytes) {
- var loc = gl.getAttribLocation(program, attribute);
- if (loc === -1) {
- return false;
- }
- callAndCheck(gl, function () { return gl.bindBuffer(gl.ARRAY_BUFFER, buffer); });
- callAndCheck(gl, function () { return gl.vertexAttribPointer(loc, arrayEntriesPerItem, gl.FLOAT, false, itemStrideInBytes, itemOffsetInBytes); });
- callAndCheck(gl, function () { return gl.enableVertexAttribArray(loc); });
- return true;
- }
- function bindTextureUnit(gl, texture, textureUnit) {
- validateTextureUnit(gl, textureUnit);
- callAndCheck(gl, function () { return gl.activeTexture(gl.TEXTURE0 + textureUnit); });
- callAndCheck(gl, function () { return gl.bindTexture(gl.TEXTURE_2D, texture); });
- }
- function unbindTextureUnit(gl, textureUnit) {
- validateTextureUnit(gl, textureUnit);
- callAndCheck(gl, function () { return gl.activeTexture(gl.TEXTURE0 + textureUnit); });
- callAndCheck(gl, function () { return gl.bindTexture(gl.TEXTURE_2D, null); });
- }
- function getProgramUniformLocationOrThrow(gl, program, uniformName) {
- return throwIfNull(gl, function () { return gl.getUniformLocation(program, uniformName); }, 'uniform "' + uniformName + '" not present in program.');
- }
- function getProgramUniformLocation(gl, program, uniformName) {
- return gl.getUniformLocation(program, uniformName);
- }
- function bindTextureToProgramUniformSampler(gl, program, texture, uniformSamplerLocation, textureUnit) {
- callAndCheck(gl, function () { return bindTextureUnit(gl, texture, textureUnit); });
- callAndCheck(gl, function () { return gl.uniform1i(uniformSamplerLocation, textureUnit); });
- }
- function bindCanvasToFramebuffer(gl) {
- callAndCheck(gl, function () { return gl.bindFramebuffer(gl.FRAMEBUFFER, null); });
- callAndCheck(gl, function () { return gl.viewport(0, 0, gl.canvas.width, gl.canvas.height); });
- callAndCheck(gl, function () { return gl.scissor(0, 0, gl.canvas.width, gl.canvas.height); });
- }
- function bindColorTextureToFramebuffer(gl, texture, framebuffer) {
- callAndCheck(gl, function () { return gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer); });
- callAndCheck(gl, function () { return gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); });
- }
- function unbindColorTextureFromFramebuffer(gl, framebuffer) {
- callAndCheck(gl, function () { return gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer); });
- callAndCheck(gl, function () { return gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, null, 0); });
- }
- function validateFramebuffer(gl) {
- var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
- if (status !== gl.FRAMEBUFFER_COMPLETE) {
- throw new Error('Error binding framebuffer: ' + getFramebufferErrorMessage(gl, status));
- }
- }
- function getFramebufferErrorMessage(gl, status) {
- switch (status) {
- case gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
- return 'FRAMEBUFFER_INCOMPLETE_ATTACHMENT';
- case gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
- return 'FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT';
- case gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
- return 'FRAMEBUFFER_INCOMPLETE_DIMENSIONS';
- case gl.FRAMEBUFFER_UNSUPPORTED:
- return 'FRAMEBUFFER_UNSUPPORTED';
- default:
- return "unknown error " + status;
- }
- }
- function throwIfNull(gl, returnTOrNull, failureMessage) {
- var tOrNull = callAndCheck(gl, function () { return returnTOrNull(); });
- if (tOrNull == null) {
- throw new Error(failureMessage);
- }
- return tOrNull;
- }
- function validateTextureUnit(gl, textureUnit) {
- var maxTextureUnit = gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1;
- var glTextureUnit = textureUnit + gl.TEXTURE0;
- if (glTextureUnit < gl.TEXTURE0 || glTextureUnit > maxTextureUnit) {
- var textureUnitRange = "[gl.TEXTURE0, gl.TEXTURE" + maxTextureUnit + "]";
- throw new Error("textureUnit must be in " + textureUnitRange + ".");
- }
- }
- function getTextureShapeFromLogicalShape(gl, logShape) {
- if (logShape.length !== 2) {
- var squeezeResult = squeezeShape(logShape);
- logShape = squeezeResult.newShape;
- }
- var maxTexSize = queryMaxTextureSize(gl);
- var size = sizeFromShape(logShape);
- if (logShape.length <= 1 && size <= maxTexSize) {
- return [size, 1];
- }
- else if (logShape.length === 2 && logShape[0] <= maxTexSize &&
- logShape[1] <= maxTexSize) {
- return logShape;
- }
- else if (logShape.length === 3 && logShape[0] <= maxTexSize &&
- logShape[1] * logShape[2] <= maxTexSize) {
- return [logShape[0], logShape[1] * logShape[2]];
- }
- else if (logShape.length === 4 && logShape[0] <= maxTexSize &&
- logShape[1] * logShape[2] * logShape[3] <= maxTexSize) {
- return [logShape[0], logShape[1] * logShape[2] * logShape[3]];
- }
- else {
- return sizeToSquarishShape(size);
- }
- }
-
- var webgl_util = /*#__PURE__*/Object.freeze({
- createWebGLRenderingContext: createWebGLRenderingContext,
- createWebGLRenderingContextFromCanvas: createWebGLRenderingContextFromCanvas,
- callAndCheck: callAndCheck,
- enableDebugWebGLErrorChecking: enableDebugWebGLErrorChecking,
- checkWebGLError: checkWebGLError,
- getWebGLErrorMessage: getWebGLErrorMessage,
- getExtensionOrThrow: getExtensionOrThrow,
- createVertexShader: createVertexShader,
- createFragmentShader: createFragmentShader,
- createProgram: createProgram,
- linkProgram: linkProgram,
- validateProgram: validateProgram,
- createStaticVertexBuffer: createStaticVertexBuffer,
- createStaticIndexBuffer: createStaticIndexBuffer,
- queryMaxTextureSize: queryMaxTextureSize,
- getChannelsPerTexture: getChannelsPerTexture,
- createTexture: createTexture,
- validateTextureSize: validateTextureSize,
- createFramebuffer: createFramebuffer,
- bindVertexBufferToProgramAttribute: bindVertexBufferToProgramAttribute,
- bindTextureUnit: bindTextureUnit,
- unbindTextureUnit: unbindTextureUnit,
- getProgramUniformLocationOrThrow: getProgramUniformLocationOrThrow,
- getProgramUniformLocation: getProgramUniformLocation,
- bindTextureToProgramUniformSampler: bindTextureToProgramUniformSampler,
- bindCanvasToFramebuffer: bindCanvasToFramebuffer,
- bindColorTextureToFramebuffer: bindColorTextureToFramebuffer,
- unbindColorTextureFromFramebuffer: unbindColorTextureFromFramebuffer,
- validateFramebuffer: validateFramebuffer,
- getFramebufferErrorMessage: getFramebufferErrorMessage,
- getTextureShapeFromLogicalShape: getTextureShapeFromLogicalShape
- });
-
- var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$3 = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- function getWebGLContextAttributes() {
- return {
- alpha: false,
- antialias: false,
- premultipliedAlpha: false,
- preserveDrawingBuffer: false,
- depth: false,
- stencil: false,
- failIfMajorPerformanceCaveat: true
- };
- }
- function createWebGLContext(canvas) {
- var attributes = getWebGLContextAttributes();
- var gl;
- if (canvas != null) {
- gl = createWebGLRenderingContextFromCanvas(canvas, attributes);
- }
- else {
- gl = createWebGLRenderingContext(attributes);
- }
- callAndCheck(gl, function () { return gl.disable(gl.DEPTH_TEST); });
- callAndCheck(gl, function () { return gl.disable(gl.STENCIL_TEST); });
- callAndCheck(gl, function () { return gl.disable(gl.BLEND); });
- callAndCheck(gl, function () { return gl.disable(gl.DITHER); });
- callAndCheck(gl, function () { return gl.disable(gl.POLYGON_OFFSET_FILL); });
- callAndCheck(gl, function () { return gl.disable(gl.SAMPLE_COVERAGE); });
- callAndCheck(gl, function () { return gl.enable(gl.SCISSOR_TEST); });
- callAndCheck(gl, function () { return gl.enable(gl.CULL_FACE); });
- callAndCheck(gl, function () { return gl.cullFace(gl.BACK); });
- return gl;
- }
- function createVertexShader$1(gl) {
- var vertexShaderSource = "\n precision highp float;\n attribute vec3 clipSpacePos;\n attribute vec2 uv;\n varying vec2 resultUV;\n\n void main() {\n gl_Position = vec4(clipSpacePos, 1);\n resultUV = uv;\n }";
- return createVertexShader(gl, vertexShaderSource);
- }
- function createVertexBuffer(gl) {
- var vertexArray = new Float32Array([-1, 1, 0, 0, 1, -1, -1, 0, 0, 0, 1, 1, 0, 1, 1, 1, -1, 0, 1, 0]);
- return createStaticVertexBuffer(gl, vertexArray);
- }
- function createIndexBuffer(gl) {
- var triangleVertexIndices = new Uint16Array([0, 1, 2, 2, 1, 3]);
- return createStaticIndexBuffer(gl, triangleVertexIndices);
- }
- function getTextureInternalFormat(gl, numChannels) {
- if (!ENV.get('WEBGL_FLOAT_TEXTURE_ENABLED')) {
- return gl.RGBA;
- }
- if (ENV.get('WEBGL_VERSION') === 2) {
- if (numChannels === 4) {
- return gl.RGBA32F;
- }
- return gl.R32F;
- }
- return gl.RGBA;
- }
- function getTextureFormat(gl, numChannels) {
- if (!ENV.get('WEBGL_FLOAT_TEXTURE_ENABLED')) {
- return gl.RGBA;
- }
- if (ENV.get('WEBGL_VERSION') === 2) {
- if (numChannels === 4) {
- return gl.RGBA;
- }
- return gl.RED;
- }
- return gl.RGBA;
- }
- function getTextureType(gl) {
- if (!ENV.get('WEBGL_FLOAT_TEXTURE_ENABLED')) {
- return gl.UNSIGNED_BYTE;
- }
- return gl.FLOAT;
- }
- function createAndConfigureTexture(gl, width, height, numChannels) {
- validateTextureSize(gl, width, height);
- var texture = createTexture(gl);
- var tex2d = gl.TEXTURE_2D;
- var internalFormat = getTextureInternalFormat(gl, numChannels);
- var format = getTextureFormat(gl, numChannels);
- callAndCheck(gl, function () { return gl.bindTexture(tex2d, texture); });
- callAndCheck(gl, function () { return gl.texParameteri(tex2d, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); });
- callAndCheck(gl, function () { return gl.texParameteri(tex2d, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); });
- callAndCheck(gl, function () { return gl.texParameteri(tex2d, gl.TEXTURE_MIN_FILTER, gl.NEAREST); });
- callAndCheck(gl, function () { return gl.texParameteri(tex2d, gl.TEXTURE_MAG_FILTER, gl.NEAREST); });
- callAndCheck(gl, function () { return gl.texImage2D(tex2d, 0, internalFormat, width, height, 0, format, getTextureType(gl), null); });
- callAndCheck(gl, function () { return gl.bindTexture(gl.TEXTURE_2D, null); });
- return texture;
- }
- function createMatrixTexture(gl, rows, columns) {
- var _a = getUnpackedMatrixTextureShapeWidthHeight(rows, columns), width = _a[0], height = _a[1];
- var numChannels = 1;
- return createAndConfigureTexture(gl, width, height, numChannels);
- }
- function createColorMatrixTexture(gl, rows, columns) {
- var _a = getColorMatrixTextureShapeWidthHeight(rows, columns), width = _a[0], height = _a[1];
- var numChannels = 4;
- return createAndConfigureTexture(gl, width, height, numChannels);
- }
- function createPackedMatrixTexture(gl, rows, columns) {
- var _a = getPackedMatrixTextureShapeWidthHeight(rows, columns), width = _a[0], height = _a[1];
- var numChannels = 4;
- return createAndConfigureTexture(gl, width, height, numChannels);
- }
- function bindVertexProgramAttributeStreams(gl, program, vertexBuffer) {
- var posOffset = 0;
- var uvOffset = 3 * 4;
- var stride = (3 * 4) + (2 * 4);
- callAndCheck(gl, function () { return gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); });
- var success = bindVertexBufferToProgramAttribute(gl, program, 'clipSpacePos', vertexBuffer, 3, stride, posOffset);
- return success &&
- bindVertexBufferToProgramAttribute(gl, program, 'uv', vertexBuffer, 2, stride, uvOffset);
- }
- function uploadPixelDataToTexture(gl, texture, pixels) {
- callAndCheck(gl, function () { return gl.bindTexture(gl.TEXTURE_2D, texture); });
- callAndCheck(gl, function () { return gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, pixels); });
- callAndCheck(gl, function () { return gl.bindTexture(gl.TEXTURE_2D, null); });
- }
- function uploadDataToTexture(gl, texture, width, height, data, numChannels) {
- var textureFormat = getTextureFormat(gl, numChannels);
- validateTextureSize(gl, width, height);
- callAndCheck(gl, function () { return gl.bindTexture(gl.TEXTURE_2D, texture); });
- callAndCheck(gl, function () { return gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, textureFormat, getTextureType(gl), data); });
- callAndCheck(gl, function () { return gl.bindTexture(gl.TEXTURE_2D, null); });
- }
- function uploadMatrixToTexture(gl, texture, rows, columns, matrix, numChannels) {
- var _a = getUnpackedMatrixTextureShapeWidthHeight(rows, columns), w = _a[0], h = _a[1];
- var unpackedArray;
- if (ENV.get('WEBGL_FLOAT_TEXTURE_ENABLED')) {
- var channelsPerTexture = numChannels === 1 ? getChannelsPerTexture() : numChannels;
- if (channelsPerTexture === 1) {
- unpackedArray = matrix;
- }
- else {
- unpackedArray =
- new Float32Array(getUnpackedArraySizeFromMatrixSize(matrix.length, channelsPerTexture));
- encodeMatrixToUnpackedArray(matrix, unpackedArray, channelsPerTexture);
- }
- }
- else {
- unpackedArray = encodeFloatArray(matrix);
- }
- uploadDataToTexture(gl, texture, w, h, unpackedArray, numChannels);
- }
- function uploadMatrixToPackedTexture(gl, texture, rows, columns, matrix) {
- var _a = getPackedMatrixTextureShapeWidthHeight(rows, columns), w = _a[0], h = _a[1];
- var packedRGBA = new Float32Array(getPackedRGBAArraySizeFromMatrixShape(rows, columns));
- encodeMatrixToPackedRGBA(matrix, rows, columns, packedRGBA);
- var numChannels = 4;
- uploadDataToTexture(gl, texture, w, h, packedRGBA, numChannels);
- }
- function getDownloadTargetArrayBuffer(rows, columns, channelsPerTexture) {
- var isFloatTexture = ENV.get('WEBGL_FLOAT_TEXTURE_ENABLED');
- var downloadTarget;
- if (isFloatTexture) {
- downloadTarget =
- new Float32Array(getUnpackedArraySizeFromMatrixSize(rows * columns, channelsPerTexture));
- }
- else {
- downloadTarget = new Uint8Array(rows * columns * channelsPerTexture);
- }
- return downloadTarget;
- }
- function decodeDownloadTargetArrayBuffer(downloadTarget, rows, columns, channelsPerPixel) {
- var isFloatTexture = ENV.get('WEBGL_FLOAT_TEXTURE_ENABLED');
- if (isFloatTexture) {
- var matrix = new Float32Array(rows * columns);
- decodeMatrixFromUnpackedArray(downloadTarget, matrix, channelsPerPixel);
- return matrix;
- }
- else {
- return decodeToFloatArray(downloadTarget);
- }
- }
- function downloadMatrixFromOutputTextureAsync(gl, getBufferSubDataAsyncExtension, rows, columns) {
- return __awaiter$3(this, void 0, void 0, function () {
- var gl2, channelsPerPixel, downloadTarget, bufferSizeBytes, buffer;
- return __generator$3(this, function (_a) {
- switch (_a.label) {
- case 0:
- gl2 = gl;
- channelsPerPixel = 4;
- downloadTarget = getDownloadTargetArrayBuffer(rows, columns, channelsPerPixel);
- bufferSizeBytes = downloadTarget instanceof Float32Array ?
- downloadTarget.length * 4 :
- downloadTarget;
- buffer = gl.createBuffer();
- callAndCheck(gl, function () { return gl.bindBuffer(gl2.PIXEL_PACK_BUFFER, buffer); });
- callAndCheck(gl, function () { return gl.bufferData(gl2.PIXEL_PACK_BUFFER, bufferSizeBytes, gl.STATIC_DRAW); });
- callAndCheck(gl, function () {
- return gl2.readPixels(0, 0, columns, rows, gl.RGBA, getTextureType(gl), 0);
- });
- return [4, getBufferSubDataAsyncExtension.getBufferSubDataAsync(gl2.PIXEL_PACK_BUFFER, 0, downloadTarget)];
- case 1:
- _a.sent();
- return [2, decodeDownloadTargetArrayBuffer(downloadTarget, rows, columns, channelsPerPixel)];
- }
- });
- });
- }
- function downloadMatrixFromOutputTexture(gl, rows, columns) {
- var _a = getUnpackedMatrixTextureShapeWidthHeight(rows, columns), w = _a[0], h = _a[1];
- var channelsPerPixel = 4;
- var downloadTarget = getDownloadTargetArrayBuffer(rows, columns, channelsPerPixel);
- callAndCheck(gl, function () { return gl.readPixels(0, 0, w, h, gl.RGBA, getTextureType(gl), downloadTarget); });
- return decodeDownloadTargetArrayBuffer(downloadTarget, rows, columns, channelsPerPixel);
- }
- function downloadMatrixFromRGBAColorTexture(gl, rows, columns, channels) {
- var size = rows * columns * 4;
- var downloadTarget = new Uint8Array(size);
- callAndCheck(gl, function () { return gl.readPixels(0, 0, columns, rows, gl.RGBA, gl.UNSIGNED_BYTE, downloadTarget); });
- var packedRGBA = new Float32Array(size);
- for (var i = 0; i < downloadTarget.length; i++) {
- packedRGBA[i] = downloadTarget[i];
- }
- var matrix = new Float32Array(rows * columns * channels);
- decodeMatrixFromUnpackedColorRGBAArray(packedRGBA, matrix, channels);
- return matrix;
- }
- function downloadMatrixFromPackedOutputTexture(gl, rows, columns) {
- var _a = getPackedMatrixTextureShapeWidthHeight(rows, columns), w = _a[0], h = _a[1];
- var packedRGBA = new Float32Array(getPackedRGBAArraySizeFromMatrixShape(rows, columns));
- callAndCheck(gl, function () { return gl.readPixels(0, 0, w, h, gl.RGBA, getTextureType(gl), packedRGBA); });
- var matrix = new Float32Array(rows * columns);
- return decodeMatrixFromPackedRGBA(packedRGBA, rows, columns, matrix);
- }
-
- var gpgpu_util = /*#__PURE__*/Object.freeze({
- getWebGLContextAttributes: getWebGLContextAttributes,
- createWebGLContext: createWebGLContext,
- createVertexShader: createVertexShader$1,
- createVertexBuffer: createVertexBuffer,
- createIndexBuffer: createIndexBuffer,
- createMatrixTexture: createMatrixTexture,
- createColorMatrixTexture: createColorMatrixTexture,
- createPackedMatrixTexture: createPackedMatrixTexture,
- bindVertexProgramAttributeStreams: bindVertexProgramAttributeStreams,
- uploadPixelDataToTexture: uploadPixelDataToTexture,
- uploadMatrixToTexture: uploadMatrixToTexture,
- uploadMatrixToPackedTexture: uploadMatrixToPackedTexture,
- downloadMatrixFromOutputTextureAsync: downloadMatrixFromOutputTextureAsync,
- downloadMatrixFromOutputTexture: downloadMatrixFromOutputTexture,
- downloadMatrixFromRGBAColorTexture: downloadMatrixFromRGBAColorTexture,
- downloadMatrixFromPackedOutputTexture: downloadMatrixFromPackedOutputTexture
- });
-
- var __awaiter$4 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$4 = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var GPGPUContext = (function () {
- function GPGPUContext(gl) {
- this.outputTexture = null;
- this.program = null;
- this.disposed = false;
- this.autoDebugValidate = false;
- this.vertexAttrsAreBound = false;
- if (gl != null) {
- this.gl = gl;
- }
- else {
- this.gl = createWebGLContext();
- }
- if (ENV.get('WEBGL_VERSION') === 1) {
- this.textureFloatExtension =
- getExtensionOrThrow(this.gl, 'OES_texture_float');
- this.colorBufferFloatExtension =
- this.gl.getExtension('WEBGL_color_buffer_float');
- }
- else {
- this.colorBufferFloatExtension =
- getExtensionOrThrow(this.gl, 'EXT_color_buffer_float');
- }
- this.loseContextExtension =
- getExtensionOrThrow(this.gl, 'WEBGL_lose_context');
- if (ENV.get('WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED')) {
- this.getBufferSubDataAsyncExtension =
- this.gl.getExtension('WEBGL_get_buffer_sub_data_async');
- }
- this.vertexBuffer = createVertexBuffer(this.gl);
- this.indexBuffer = createIndexBuffer(this.gl);
- this.framebuffer = createFramebuffer(this.gl);
- }
- GPGPUContext.prototype.dispose = function () {
- var _this = this;
- if (this.disposed) {
- return;
- }
- if (this.program != null) {
- 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.');
- }
- if (this.outputTexture != null) {
- 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 gl = this.gl;
- callAndCheck(gl, function () { return gl.finish(); });
- callAndCheck(gl, function () { return gl.bindFramebuffer(gl.FRAMEBUFFER, null); });
- callAndCheck(gl, function () { return gl.deleteFramebuffer(_this.framebuffer); });
- callAndCheck(gl, function () { return gl.bindBuffer(gl.ARRAY_BUFFER, null); });
- callAndCheck(gl, function () { return gl.deleteBuffer(_this.vertexBuffer); });
- callAndCheck(gl, function () { return gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null); });
- callAndCheck(gl, function () { return gl.deleteBuffer(_this.indexBuffer); });
- this.loseContextExtension.loseContext();
- this.disposed = true;
- };
- GPGPUContext.prototype.enableAutomaticDebugValidation = function (enabled) {
- this.autoDebugValidate = enabled;
- enableDebugWebGLErrorChecking(enabled);
- };
- GPGPUContext.prototype.createMatrixTexture = function (rows, columns) {
- this.throwIfDisposed();
- return createMatrixTexture(this.gl, rows, columns);
- };
- GPGPUContext.prototype.uploadPixelDataToTexture = function (texture, pixels) {
- this.throwIfDisposed();
- uploadPixelDataToTexture(this.gl, texture, pixels);
- };
- GPGPUContext.prototype.createPackedMatrixTexture = function (rows, columns) {
- this.throwIfDisposed();
- return createPackedMatrixTexture(this.gl, rows, columns);
- };
- GPGPUContext.prototype.deleteMatrixTexture = function (texture) {
- var _this = this;
- this.throwIfDisposed();
- if (this.outputTexture === texture) {
- unbindColorTextureFromFramebuffer(this.gl, this.framebuffer);
- this.outputTexture = null;
- }
- callAndCheck(this.gl, function () { return _this.gl.deleteTexture(texture); });
- };
- GPGPUContext.prototype.uploadMatrixToTexture = function (texture, rows, columns, matrix) {
- this.throwIfDisposed();
- var numChannels = 1;
- return uploadMatrixToTexture(this.gl, texture, rows, columns, matrix, numChannels);
- };
- GPGPUContext.prototype.uploadMatrixToPackedTexture = function (texture, rows, columns, matrix) {
- this.throwIfDisposed();
- return uploadMatrixToPackedTexture(this.gl, texture, rows, columns, matrix);
- };
- GPGPUContext.prototype.downloadMatrixFromTexture = function (texture, rows, columns) {
- var _this = this;
- return this.downloadMatrixDriver(texture, function () {
- return downloadMatrixFromOutputTexture(_this.gl, rows, columns);
- });
- };
- GPGPUContext.prototype.downloadMatrixFromTextureAsync = function (texture, rows, columns) {
- return __awaiter$4(this, void 0, void 0, function () {
- var _this = this;
- return __generator$4(this, function (_a) {
- if (this.getBufferSubDataAsyncExtension == null) {
- throw new Error("Cannot download matrix from output texture asynchronously, " +
- "WEBGL_get_buffer_sub_data_async is not enabled.");
- }
- return [2, this.downloadMatrixDriverAsync(texture, function () { return downloadMatrixFromOutputTextureAsync(_this.gl, _this.getBufferSubDataAsyncExtension, rows, columns); })];
- });
- });
- };
- GPGPUContext.prototype.downloadMatrixFromRGBAColorTexture = function (texture, rows, columns, channels) {
- var _this = this;
- return this.downloadMatrixDriver(texture, function () { return downloadMatrixFromRGBAColorTexture(_this.gl, rows, columns, channels); });
- };
- GPGPUContext.prototype.downloadMatrixFromPackedTexture = function (texture, rows, columns) {
- var _this = this;
- return this.downloadMatrixDriver(texture, function () { return downloadMatrixFromPackedOutputTexture(_this.gl, rows, columns); });
- };
- GPGPUContext.prototype.createProgram = function (fragmentShaderSource) {
- this.throwIfDisposed();
- var gl = this.gl;
- var fragmentShader = createFragmentShader(gl, fragmentShaderSource);
- var vertexShader = createVertexShader$1(gl);
- var program = createProgram(gl);
- callAndCheck(gl, function () { return gl.attachShader(program, vertexShader); });
- callAndCheck(gl, function () { return gl.attachShader(program, fragmentShader); });
- linkProgram(gl, program);
- if (this.autoDebugValidate) {
- validateProgram(gl, program);
- }
- if (!this.vertexAttrsAreBound) {
- this.setProgram(program);
- this.vertexAttrsAreBound = bindVertexProgramAttributeStreams(gl, this.program, this.vertexBuffer);
- }
- return program;
- };
- GPGPUContext.prototype.deleteProgram = function (program) {
- var _this = this;
- this.throwIfDisposed();
- if (program === this.program) {
- this.program = null;
- }
- if (program != null) {
- callAndCheck(this.gl, function () { return _this.gl.deleteProgram(program); });
- }
- };
- GPGPUContext.prototype.setProgram = function (program) {
- var _this = this;
- this.throwIfDisposed();
- this.program = program;
- if ((this.program != null) && this.autoDebugValidate) {
- validateProgram(this.gl, this.program);
- }
- callAndCheck(this.gl, function () { return _this.gl.useProgram(program); });
- };
- GPGPUContext.prototype.getUniformLocation = function (program, uniformName, shouldThrow) {
- if (shouldThrow === void 0) { shouldThrow = true; }
- this.throwIfDisposed();
- if (shouldThrow) {
- return getProgramUniformLocationOrThrow(this.gl, program, uniformName);
- }
- else {
- return getProgramUniformLocation(this.gl, program, uniformName);
- }
- };
- GPGPUContext.prototype.getAttributeLocation = function (program, attribute) {
- var _this = this;
- this.throwIfDisposed();
- return callAndCheck(this.gl, function () { return _this.gl.getAttribLocation(program, attribute); });
- };
- GPGPUContext.prototype.getUniformLocationNoThrow = function (program, uniformName) {
- this.throwIfDisposed();
- return this.gl.getUniformLocation(program, uniformName);
- };
- GPGPUContext.prototype.setInputMatrixTexture = function (inputMatrixTexture, uniformLocation, textureUnit) {
- this.throwIfDisposed();
- this.throwIfNoProgram();
- bindTextureToProgramUniformSampler(this.gl, this.program, inputMatrixTexture, uniformLocation, textureUnit);
- };
- GPGPUContext.prototype.setOutputMatrixTexture = function (outputMatrixTexture, rows, columns) {
- this.setOutputMatrixTextureDriver(outputMatrixTexture, columns, rows);
- };
- GPGPUContext.prototype.setOutputPackedMatrixTexture = function (outputPackedMatrixTexture, rows, columns) {
- this.throwIfDisposed();
- var _a = getPackedMatrixTextureShapeWidthHeight(rows, columns), width = _a[0], height = _a[1];
- this.setOutputMatrixTextureDriver(outputPackedMatrixTexture, width, height);
- };
- GPGPUContext.prototype.setOutputMatrixWriteRegion = function (startRow, numRows, startColumn, numColumns) {
- this.setOutputMatrixWriteRegionDriver(startColumn, startRow, numColumns, numRows);
- };
- GPGPUContext.prototype.setOutputPackedMatrixWriteRegion = function (startRow, numRows, startColumn, numColumns) {
- throw new Error('setOutputPackedMatrixWriteRegion not implemented.');
- };
- GPGPUContext.prototype.debugValidate = function () {
- if (this.program != null) {
- validateProgram(this.gl, this.program);
- }
- validateFramebuffer(this.gl);
- };
- GPGPUContext.prototype.executeProgram = function () {
- this.throwIfDisposed();
- this.throwIfNoProgram();
- var gl = this.gl;
- if (this.autoDebugValidate) {
- this.debugValidate();
- }
- callAndCheck(gl, function () { return gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0); });
- };
- GPGPUContext.prototype.blockUntilAllProgramsCompleted = function () {
- var _this = this;
- this.throwIfDisposed();
- callAndCheck(this.gl, function () { return _this.gl.finish(); });
- };
- GPGPUContext.prototype.getQueryTimerExtension = function () {
- if (this.disjointQueryTimerExtension == null) {
- this.disjointQueryTimerExtension =
- getExtensionOrThrow(this.gl, ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION') === 2 ?
- 'EXT_disjoint_timer_query_webgl2' :
- 'EXT_disjoint_timer_query');
- }
- return this.disjointQueryTimerExtension;
- };
- GPGPUContext.prototype.getQueryTimerExtensionWebGL2 = function () {
- return this.getQueryTimerExtension();
- };
- GPGPUContext.prototype.getQueryTimerExtensionWebGL1 = function () {
- return this.getQueryTimerExtension();
- };
- GPGPUContext.prototype.runQuery = function (queryFn) {
- var query = this.beginQuery();
- queryFn();
- this.endQuery();
- return this.pollQueryTime(query);
- };
- GPGPUContext.prototype.beginQuery = function () {
- if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION') === 2) {
- var gl2 = this.gl;
- var ext_1 = this.getQueryTimerExtensionWebGL2();
- var query_1 = gl2.createQuery();
- gl2.beginQuery(ext_1.TIME_ELAPSED_EXT, query_1);
- return query_1;
- }
- var ext = this.getQueryTimerExtensionWebGL1();
- var query = ext.createQueryEXT();
- ext.beginQueryEXT(ext.TIME_ELAPSED_EXT, query);
- return query;
- };
- GPGPUContext.prototype.endQuery = function () {
- if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION') === 2) {
- var gl2 = this.gl;
- var ext_2 = this.getQueryTimerExtensionWebGL2();
- gl2.endQuery(ext_2.TIME_ELAPSED_EXT);
- return;
- }
- var ext = this.getQueryTimerExtensionWebGL1();
- ext.endQueryEXT(ext.TIME_ELAPSED_EXT);
- };
- GPGPUContext.prototype.isQueryAvailable = function (query, queryTimerVersion) {
- if (queryTimerVersion === 0) {
- return true;
- }
- if (queryTimerVersion === 2) {
- var gl2 = this.gl;
- var ext = this.getQueryTimerExtensionWebGL2();
- var available = gl2.getQueryParameter(query, gl2.QUERY_RESULT_AVAILABLE);
- var disjoint = this.gl.getParameter(ext.GPU_DISJOINT_EXT);
- return available && !disjoint;
- }
- else {
- var ext = this.getQueryTimerExtensionWebGL1();
- var available = ext.getQueryObjectEXT(query, ext.QUERY_RESULT_AVAILABLE_EXT);
- var disjoint = this.gl.getParameter(ext.GPU_DISJOINT_EXT);
- return available && !disjoint;
- }
- };
- GPGPUContext.prototype.pollQueryTime = function (query) {
- var _this = this;
- return new Promise(function (resolve, reject) {
- var resolveWithWarning = function () {
- console.warn('Disjoint query timer never available.');
- resolve(-1);
- };
- var queryTimerVersion = ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION');
- repeatedTry(function () { return _this.isQueryAvailable(query, queryTimerVersion); })
- .then(function () { return resolve(_this.getQueryTime(query, queryTimerVersion)); })
- .catch(resolveWithWarning);
- });
- };
- GPGPUContext.prototype.getQueryTime = function (query, queryTimerVersion) {
- if (queryTimerVersion === 0) {
- return null;
- }
- if (queryTimerVersion === 2) {
- var gl2 = this.gl;
- var timeElapsedNanos = gl2.getQueryParameter(query, gl2.QUERY_RESULT);
- return timeElapsedNanos / 1000000;
- }
- else {
- var ext = this.getQueryTimerExtensionWebGL1();
- var timeElapsedNanos = ext.getQueryObjectEXT(query, ext.QUERY_RESULT_EXT);
- return timeElapsedNanos / 1000000;
- }
- };
- GPGPUContext.prototype.downloadMatrixDriverSetup = function (texture) {
- this.throwIfDisposed();
- bindColorTextureToFramebuffer(this.gl, texture, this.framebuffer);
- if (this.autoDebugValidate) {
- validateFramebuffer(this.gl);
- }
- };
- GPGPUContext.prototype.downloadMatrixDriverTeardown = function () {
- if (this.outputTexture != null) {
- bindColorTextureToFramebuffer(this.gl, this.outputTexture, this.framebuffer);
- if (this.autoDebugValidate) {
- validateFramebuffer(this.gl);
- }
- }
- else {
- unbindColorTextureFromFramebuffer(this.gl, this.framebuffer);
- }
- };
- GPGPUContext.prototype.downloadMatrixDriver = function (texture, downloadAndDecode) {
- this.downloadMatrixDriverSetup(texture);
- var result = downloadAndDecode();
- this.downloadMatrixDriverTeardown();
- return result;
- };
- GPGPUContext.prototype.downloadMatrixDriverAsync = function (texture, downloadAndDecode) {
- return __awaiter$4(this, void 0, void 0, function () {
- var result;
- return __generator$4(this, function (_a) {
- switch (_a.label) {
- case 0:
- this.downloadMatrixDriverSetup(texture);
- return [4, downloadAndDecode()];
- case 1:
- result = _a.sent();
- this.downloadMatrixDriverTeardown();
- return [2, result];
- }
- });
- });
- };
- GPGPUContext.prototype.setOutputMatrixTextureDriver = function (outputMatrixTextureMaybePacked, width, height) {
- this.throwIfDisposed();
- var gl = this.gl;
- bindColorTextureToFramebuffer(gl, outputMatrixTextureMaybePacked, this.framebuffer);
- if (this.autoDebugValidate) {
- validateFramebuffer(gl);
- }
- this.outputTexture = outputMatrixTextureMaybePacked;
- callAndCheck(gl, function () { return gl.viewport(0, 0, width, height); });
- callAndCheck(gl, function () { return gl.scissor(0, 0, width, height); });
- };
- GPGPUContext.prototype.setOutputMatrixWriteRegionDriver = function (x, y, width, height) {
- var _this = this;
- this.throwIfDisposed();
- callAndCheck(this.gl, function () { return _this.gl.scissor(x, y, width, height); });
- };
- GPGPUContext.prototype.throwIfDisposed = function () {
- if (this.disposed) {
- throw new Error('Attempted to use disposed GPGPUContext.');
- }
- };
- GPGPUContext.prototype.throwIfNoProgram = function () {
- if (this.program == null) {
- throw new Error('No GPU program is currently set.');
- }
- };
- return GPGPUContext;
- }());
-
- var NAN_UNIFORM_NAME = 'NaN';
- function shouldUploadNaNUniform() {
- return !ENV.get('WEBGL_FLOAT_TEXTURE_ENABLED');
- }
- function compileProgram(gpgpu, program, inputs, output) {
- var userCode = program.userCode;
- var inputInfos = inputs.map(function (input, i) {
- var shapeInfo = {
- logicalShape: input.tensor.shape,
- texShape: input.texData.texShape
- };
- return { name: program.variableNames[i], shapeInfo: shapeInfo };
- });
- var inShapeInfos = inputInfos.map(function (x) { return x.shapeInfo; });
- var outShapeInfo = {
- logicalShape: output.tensor.shape,
- texShape: output.texData.texShape
- };
- var source = makeShader(inputInfos, outShapeInfo, userCode, program.supportsBroadcasting === true);
- var webGLProgram = gpgpu.createProgram(source);
- var uniformLocations = {};
- for (var i = 0; i < program.variableNames.length; i++) {
- var uniformName = program.variableNames[i];
- uniformLocations[uniformName] =
- gpgpu.getUniformLocation(webGLProgram, uniformName);
- }
- if (shouldUploadNaNUniform()) {
- var throwIfNaNUniformIsNotUsed = false;
- uniformLocations[NAN_UNIFORM_NAME] = gpgpu.getUniformLocation(webGLProgram, NAN_UNIFORM_NAME, throwIfNaNUniformIsNotUsed);
- }
- return {
- program: program,
- source: source,
- webGLProgram: webGLProgram,
- uniformLocations: uniformLocations,
- gpgpu: gpgpu,
- inShapeInfos: inShapeInfos,
- outShapeInfo: outShapeInfo
- };
- }
- function validateBinaryAndProgram(shapeInfos, inputs) {
- if (shapeInfos.length !== inputs.length) {
- throw Error("Binary was compiled with " + shapeInfos.length + " inputs, but " +
- ("was executed with " + inputs.length + " inputs"));
- }
- shapeInfos.forEach(function (s, i) {
- var shapeA = s.logicalShape;
- var texShapeA = s.texShape;
- var shapeB = inputs[i].tensor.shape;
- var texShapeB = inputs[i].texData.texShape;
- if (!arraysEqual(shapeA, shapeB)) {
- throw Error("Binary was compiled with different shapes than " +
- ("the current args. Shapes " + shapeA + " and " + shapeB + " must match"));
- }
- if (!arraysEqual(texShapeA, texShapeB)) {
- throw Error("Binary was compiled with different texture shapes than the" +
- (" current args. Shape " + texShapeA + " and " + texShapeB + " must match"));
- }
- });
- }
- function runProgram(binary, inputs, output, customSetup) {
- validateBinaryAndProgram(binary.inShapeInfos, inputs);
- validateBinaryAndProgram([binary.outShapeInfo], [output]);
- var outTex = output.texData.texture;
- var outTexShape = output.texData.texShape;
- var gpgpu = binary.gpgpu;
- gpgpu.setOutputMatrixTexture(outTex, outTexShape[0], outTexShape[1]);
- gpgpu.setProgram(binary.webGLProgram);
- inputs.forEach(function (input, i) {
- var tex = input.texData.texture;
- var variableName = binary.program.variableNames[i];
- var variableUniformLocation = binary.uniformLocations[variableName];
- gpgpu.setInputMatrixTexture(tex, variableUniformLocation, i);
- });
- if (shouldUploadNaNUniform()) {
- gpgpu.gl.uniform1f(binary.uniformLocations[NAN_UNIFORM_NAME], NaN);
- }
- if (customSetup != null) {
- customSetup(gpgpu, binary.webGLProgram);
- }
- gpgpu.executeProgram();
- }
- function makeShaderKey(program, inputs, output) {
- var keyInputs = '';
- inputs.concat(output).forEach(function (x) {
- keyInputs += x.tensor.shape + "_" + x.texData.texShape;
- });
- var keyUserCode = program.userCode;
- var keyBroadcast = (program.supportsBroadcasting === true).toString();
- var key = program.constructor.name;
- key += '_' + keyBroadcast + '_' + keyInputs + '_' + keyUserCode;
- return key;
- }
-
- var WhereProgram = (function () {
- function WhereProgram(cRank, shape, rank) {
- this.variableNames = ['c', 'a', 'b'];
- this.outputShape = shape;
- var cCoords;
- var abCoords;
- if (rank > 4) {
- throw Error("Where for rank " + rank + " is not yet supported");
- }
- if (rank === 1) {
- abCoords = "resRC";
- cCoords = "resRC";
- }
- else {
- var currentCoords = ['resRC.x', 'resRC.y', 'resRC.z', 'resRC.w'];
- var cCoordVars = [];
- var abCoordVars = [];
- for (var i = 0; i < shape.length; i++) {
- abCoordVars.push("" + currentCoords[i]);
- if (i < cRank) {
- cCoordVars.push("" + currentCoords[i]);
- }
- }
- cCoords = cCoordVars.join();
- abCoords = abCoordVars.join();
- }
- var dtype = getCoordsDataType(rank);
- this.userCode = "\n void main() {\n " + dtype + " resRC = getOutputCoords();\n float cVal = getC(" + cCoords + ");\n if (cVal >= 1.0) {\n setOutput(getA(" + abCoords + "));\n } else {\n setOutput(getB(" + abCoords + "));\n }\n }\n ";
- }
- return WhereProgram;
- }());
-
- var LRNProgram = (function () {
- function LRNProgram(xShape, radius, bias, alpha, beta) {
- this.variableNames = ['x'];
- this.outputShape = [];
- var rad = radius;
- var maxD = xShape[3] - 1;
- this.outputShape = xShape;
- var powOperator;
- var basis = "float(" + bias + ") + float(" + alpha + ") * sum";
- if (beta === 0.5) {
- powOperator = "inversesqrt(" + basis + ")";
- }
- else if (beta === 1.0) {
- powOperator = "1.0/(" + basis + ")";
- }
- else {
- powOperator = "exp(log(" + basis + ") * float(-" + beta + "));";
- }
- 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 = -" + rad + "; j <= " + rad + "; j++) {\n int idx = d + j;\n if (idx >= 0 && idx <= " + maxD + ") {\n float z = getX(b, r, c, idx);\n sum += z * z;\n }\n }\n float val = x * " + powOperator + ";\n setOutput(val);\n }\n ";
- }
- return LRNProgram;
- }());
-
- var MaxPool2DBackpropProgram = (function () {
- function MaxPool2DBackpropProgram(convInfo) {
- this.variableNames = ['dy', 'maxPos'];
- this.outputShape = convInfo.inShape;
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var padTop = filterHeight - 1 - convInfo.padInfo.top;
- var padLeft = filterWidth - 1 - convInfo.padInfo.left;
- var lastIndex = filterHeight * filterWidth - 1;
- this.userCode = "\n const ivec2 pads = ivec2(" + padTop + ", " + padLeft + ");\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 < " + filterHeight + "; wR++) {\n float dyR = float(dyRCorner + wR) / " + strideHeight + ".0;\n\n if (dyR < 0.0 || dyR >= " + convInfo.outHeight + ".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < " + filterWidth + "; wC++) {\n float dyC = float(dyCCorner + wC) / " + strideWidth + ".0;\n\n if (dyC < 0.0 || dyC >= " + convInfo.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 = " + lastIndex + " - 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 * " + filterWidth + " + wC;\n float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);\n\n dotProd += dyValue * mask;\n }\n }\n setOutput(dotProd);\n }\n ";
- }
- return MaxPool2DBackpropProgram;
- }());
-
- var MatMulProgram = (function () {
- function MatMulProgram(aShape, bShape, transposeA, transposeB) {
- if (transposeA === void 0) { transposeA = false; }
- if (transposeB === void 0) { transposeB = false; }
- this.variableNames = ['matrixA', 'matrixB'];
- var outerShapeA = transposeA ? aShape[1] : aShape[0];
- var outerShapeB = transposeB ? bShape[0] : bShape[1];
- var sharedDim = transposeA ? aShape[0] : aShape[1];
- this.outputShape = [outerShapeA, outerShapeB];
- var aSnippetFromOffset = function (vec4Offset, indexVar) {
- return transposeA ? indexVar + " + " + vec4Offset + ", aRow" :
- "aRow, " + indexVar + " + " + vec4Offset;
- };
- var bSnippetFromOffset = function (vec4Offset, indexVar) {
- return transposeB ? "bCol, " + indexVar + " + " + vec4Offset :
- indexVar + " + " + vec4Offset + ", bCol";
- };
- var sharedDimNearestVec4 = Math.floor(sharedDim / 4) * 4;
- var sharedDimVec4Remainder = sharedDim % 4;
- this.userCode = " float dotARowBCol(int aRow, int bCol) {\n float result = 0.0;\n for (int i = 0; i < " + sharedDimNearestVec4 + "; i += 4) {\n vec4 a = vec4(\n getMatrixA(" + aSnippetFromOffset(0, 'i') + "),\n getMatrixA(" + aSnippetFromOffset(1, 'i') + "),\n getMatrixA(" + aSnippetFromOffset(2, 'i') + "),\n getMatrixA(" + aSnippetFromOffset(3, 'i') + ")\n );\n vec4 b = vec4(\n getMatrixB(" + bSnippetFromOffset(0, 'i') + "),\n getMatrixB(" + bSnippetFromOffset(1, 'i') + "),\n getMatrixB(" + bSnippetFromOffset(2, 'i') + "),\n getMatrixB(" + bSnippetFromOffset(3, 'i') + ")\n );\n\n result += dot(a, b);\n }\n\n if (" + (sharedDimVec4Remainder === 1) + ") {\n result += getMatrixA(" + aSnippetFromOffset(0, sharedDimNearestVec4) + ") *\n getMatrixB(" + bSnippetFromOffset(0, sharedDimNearestVec4) + ");\n } else if (" + (sharedDimVec4Remainder === 2) + ") {\n vec2 a = vec2(\n getMatrixA(" + aSnippetFromOffset(0, sharedDimNearestVec4) + "),\n getMatrixA(" + aSnippetFromOffset(1, sharedDimNearestVec4) + ")\n );\n vec2 b = vec2(\n getMatrixB(" + bSnippetFromOffset(0, sharedDimNearestVec4) + "),\n getMatrixB(" + bSnippetFromOffset(1, sharedDimNearestVec4) + ")\n );\n result += dot(a, b);\n } else if (" + (sharedDimVec4Remainder === 3) + ") {\n vec3 a = vec3(\n getMatrixA(" + aSnippetFromOffset(0, sharedDimNearestVec4) + "),\n getMatrixA(" + aSnippetFromOffset(1, sharedDimNearestVec4) + "),\n getMatrixA(" + aSnippetFromOffset(2, sharedDimNearestVec4) + ")\n );\n vec3 b = vec3(\n getMatrixB(" + bSnippetFromOffset(0, sharedDimNearestVec4) + "),\n getMatrixB(" + bSnippetFromOffset(1, sharedDimNearestVec4) + "),\n getMatrixB(" + bSnippetFromOffset(2, sharedDimNearestVec4) + ")\n );\n result += dot(a, b);\n }\n\n return result;\n }\n\n void main() {\n ivec2 resRC = getOutputCoords();\n setOutput(dotARowBCol(resRC.x, resRC.y));\n }\n ";
- }
- return MatMulProgram;
- }());
-
- var MultinomialProgram = (function () {
- function MultinomialProgram(batchSize, numOutcomes, numSamples) {
- this.variableNames = ['probs'];
- this.outputShape = [batchSize, numSamples];
- 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 < " + (numOutcomes - 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(" + (numOutcomes - 1) + "));\n }\n ";
- }
- MultinomialProgram.prototype.getCustomSetupFunc = function (seed) {
- var _this = this;
- return function (gpgpu, webGLProgram) {
- if (_this.seedLoc == null) {
- _this.seedLoc = gpgpu.getUniformLocation(webGLProgram, 'seed');
- }
- gpgpu.gl.uniform1f(_this.seedLoc, seed);
- };
- };
- return MultinomialProgram;
- }());
-
- var OneHotProgram = (function () {
- function OneHotProgram(numIndices, depth, onValue, offValue) {
- this.variableNames = ['indices'];
- this.outputShape = [numIndices, depth];
- this.userCode = "\n void main() {\n ivec2 coords = getOutputCoords();\n int index = round(getIndices(coords.x));\n setOutput(mix(float(" + offValue + "), float(" + onValue + "),\n float(index == coords.y)));\n }\n ";
- }
- return OneHotProgram;
- }());
-
- var PadProgram = (function () {
- function PadProgram(xShape, paddings, constantValue) {
- this.variableNames = ['x'];
- this.outputShape = paddings.map(function (p, i) { return p[0] + xShape[i] + p[1]; });
- var rank = xShape.length;
- var type = getCoordsDataType(rank);
- var start = paddings.map(function (p) { return p[0]; }).join(',');
- var end = paddings.map(function (p, i) { return p[0] + xShape[i]; }).join(',');
- var unpackedCoords = ['coords[0]', 'coords[1]', 'coords[2]', 'coords[3]'].slice(0, rank);
- if (rank === 1) {
- this.userCode = "\n int start = " + start + ";\n int end = " + end + ";\n\n void main() {\n int outC = getOutputCoords();\n if (outC < start || outC >= end) {\n setOutput(float(" + constantValue + "));\n } else {\n setOutput(getX(outC - start));\n }\n }\n ";
- return;
- }
- this.userCode = "\n " + type + " start = " + type + "(" + start + ");\n " + type + " end = " + type + "(" + end + ");\n\n void main() {\n " + type + " outC = getOutputCoords();\n if (any(lessThan(outC, start)) || any(greaterThanEqual(outC, end))) {\n setOutput(float(" + constantValue + "));\n } else {\n " + type + " coords = outC - start;\n setOutput(getX(" + unpackedCoords + "));\n }\n }\n ";
- }
- return PadProgram;
- }());
-
- var Pool2DProgram = (function () {
- function Pool2DProgram(convInfo, poolType, computePositions) {
- this.variableNames = ['x'];
- if (poolType === 'avg' && computePositions) {
- throw new Error('Cannot compute positions for average pool.');
- }
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var padTop = convInfo.padInfo.top;
- var padLeft = convInfo.padInfo.left;
- this.outputShape = convInfo.outShape;
- var isAvgPool = poolType === 'avg';
- var initializationValue = '0.0';
- if (!isAvgPool) {
- initializationValue = '-1.0 / 0.0';
- }
- if (computePositions) {
- var compareOp_1 = '>=';
- this.userCode = "\n const ivec2 strides = ivec2(" + strideHeight + ", " + strideWidth + ");\n const ivec2 pads = ivec2(" + padTop + ", " + padLeft + ");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n float minMaxValue = 0.0;\n float minMaxValueFound = 0.0;\n int minMaxPosition = 0;\n float avgValue = 0.0;\n\n for (int wR = 0; wR < " + filterHeight + "; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= " + convInfo.inHeight + ") {\n continue;\n }\n\n for (int wC = 0; wC < " + filterWidth + "; wC++) {\n int xC = xCCorner + wC;\n\n if (xC < 0 || xC >= " + convInfo.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 " + compareOp_1 + " currMinMaxValue) {\n minMaxValue = value;\n minMaxValueFound = 1.0;\n minMaxPosition = wR * " + filterWidth + " + wC;\n }\n }\n }\n setOutput(float(minMaxPosition));\n }\n ";
- return;
- }
- var compareOp = 'max';
- var returnValue = poolType + "(" + poolType + "(" + poolType + "(" +
- 'minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])';
- if (poolType === 'avg') {
- returnValue = "avgValue / count";
- }
- var filterWidthNearestVec4 = Math.floor(filterWidth / 4) * 4;
- var filterWidthVec4Remainder = filterWidth % 4;
- var updateSnippet = "\n if (" + isAvgPool + ") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = " + compareOp + "(values, minMaxValue);\n }\n ";
- this.userCode = "\n const ivec2 strides = ivec2(" + strideHeight + ", " + strideWidth + ");\n const ivec2 pads = ivec2(" + padTop + ", " + padLeft + ");\n const float initializationValue = " + initializationValue + ";\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 >= " + convInfo.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(" + initializationValue + ");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wR = 0; wR < " + filterHeight + "; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= " + convInfo.inHeight + ") {\n continue;\n }\n\n for (int wC = 0; wC < " + filterWidthNearestVec4 + "; wC += 4) {\n int xC = xCCorner + wC;\n\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n getValue(batch, xR, xC + 3, d)\n );\n\n " + updateSnippet + "\n }\n\n int xC = xCCorner + " + filterWidthNearestVec4 + ";\n if (" + (filterWidthVec4Remainder === 1) + ") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n " + updateSnippet + "\n } else if (" + (filterWidthVec4Remainder === 2) + ") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n initializationValue,\n initializationValue\n );\n\n " + updateSnippet + "\n } else if (" + (filterWidthVec4Remainder === 3) + ") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n initializationValue\n );\n\n " + updateSnippet + "\n }\n }\n setOutput(" + returnValue + ");\n }\n ";
- }
- return Pool2DProgram;
- }());
-
- var ReduceProgram = (function () {
- function ReduceProgram(reduceInfo, reduceType) {
- this.variableNames = ['x'];
- var windowSize = reduceInfo.windowSize;
- var batchSize = reduceInfo.batchSize;
- var inSize = reduceInfo.inSize;
- var outSize = Math.ceil(inSize / windowSize);
- this.outputShape = [batchSize, outSize];
- var isReduceSum = reduceType === 'sum';
- var initializationValue = '0.0';
- if (!isReduceSum) {
- if (reduceType === 'min') {
- initializationValue = '1.0 / 0.0';
- }
- else {
- initializationValue = '-1.0 / 0.0';
- }
- }
- var compareOp = reduceType === 'min' ? 'min' : 'max';
- var returnValue = reduceType + "(" + reduceType + "(" + reduceType + "(" +
- 'minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])';
- if (reduceType === 'sum') {
- returnValue = "sumValue";
- }
- var windowSizeNearestVec4 = Math.floor(windowSize / 4) * 4;
- var windowSizeVec4Remainder = windowSize % 4;
- var updateSnippet = "\n if (" + isReduceSum + ") {\n sumValue += dot(values, ones);\n } else {\n minMaxValue = " + compareOp + "(values, minMaxValue);\n }\n ";
- var checkOutOfBounds = '';
- if (inSize % windowSize > 0) {
- checkOutOfBounds = "\n if (inIdx < 0 || inIdx >= " + inSize + ") {\n return initializationValue;\n }\n ";
- }
- this.userCode = "\n const float initializationValue = " + initializationValue + ";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float getValue(int batch, int inIdx) {\n " + checkOutOfBounds + "\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 * " + windowSize + ";\n\n vec4 minMaxValue = vec4(" + initializationValue + ");\n float sumValue = 0.0;\n\n for (int i = 0; i < " + windowSizeNearestVec4 + "; 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 " + updateSnippet + "\n }\n\n int inIdx = inOffset + " + windowSizeNearestVec4 + ";\n if (" + (windowSizeVec4Remainder === 1) + ") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n " + updateSnippet + "\n } else if (" + (windowSizeVec4Remainder === 2) + ") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n " + updateSnippet + "\n } else if (" + (windowSizeVec4Remainder === 3) + ") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n " + updateSnippet + "\n }\n setOutput(" + returnValue + ");\n }\n ";
- }
- return ReduceProgram;
- }());
-
- var ResizeBilinearBackpropProgram = (function () {
- function ResizeBilinearBackpropProgram(dy, x, alignCorners) {
- this.variableNames = ['dy'];
- this.outputShape = [];
- this.outputShape = x.shape;
- var _a = x.shape, xHeight = _a[1], xWidth = _a[2];
- var _b = dy.shape, yHeight = _b[1], yWidth = _b[2];
- var effectiveXSize = [
- (alignCorners && yHeight > 1) ? xHeight - 1 : xHeight,
- (alignCorners && yWidth > 1) ? xWidth - 1 : xWidth
- ];
- var effectiveYSize = [
- (alignCorners && yHeight > 1) ? yHeight - 1 : yHeight,
- (alignCorners && yWidth > 1) ? yWidth - 1 : yWidth
- ];
- var heightScale = effectiveXSize[0] / effectiveYSize[0];
- var widthScale = effectiveXSize[1] / effectiveYSize[1];
- var invHeightScale = 1 / heightScale;
- var invWidthScale = 1 / widthScale;
- var winHeight = (Math.ceil(invHeightScale) * 2) + 2;
- var winWidth = (Math.ceil(invWidthScale) * 2) + 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(" + heightScale + ");\n const float widthScale = float(" + widthScale + ");\n\n const float invHeightScale = float(" + invHeightScale + ");\n const float invWidthScale = float(" + invWidthScale + ");\n\n const int winHeight = int(" + winHeight + ");\n const int winWidth = int(" + winWidth + ");\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 >= " + yHeight + ") {\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 >= " + yWidth + ") {\n continue;\n }\n\n float dxR = float(dyR) * heightScale;\n int topDxRIndex = int(floor(dxR));\n int bottomDxRIndex = int(min(ceil(dxR), " + (xHeight - 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), " + (xWidth - 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 ";
- }
- return ResizeBilinearBackpropProgram;
- }());
-
- var ResizeBilinearProgram = (function () {
- function ResizeBilinearProgram(inputShape, newHeight, newWidth, alignCorners) {
- this.variableNames = ['A'];
- this.outputShape = [];
- var batch = inputShape[0], oldHeight = inputShape[1], oldWidth = inputShape[2], depth = inputShape[3];
- this.outputShape = [batch, newHeight, newWidth, depth];
- var effectiveInSize = [
- (alignCorners && newHeight > 1) ? oldHeight - 1 : oldHeight,
- (alignCorners && newWidth > 1) ? oldWidth - 1 : oldWidth
- ];
- var effectiveOutSize = [
- (alignCorners && newHeight > 1) ? newHeight - 1 : newHeight,
- (alignCorners && newWidth > 1) ? newWidth - 1 : newWidth
- ];
- this.userCode = "\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n " + effectiveInSize[0] / effectiveOutSize[0] + ",\n " + effectiveInSize[1] / effectiveOutSize[1] + ");\n const vec2 inputShapeRC = vec2(" + oldHeight + ".0, " + oldWidth + ".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 ";
- }
- return ResizeBilinearProgram;
- }());
-
- var ResizeNearestNeighborProgram = (function () {
- function ResizeNearestNeighborProgram(inputShape, newHeight, newWidth, alignCorners) {
- this.variableNames = ['A'];
- this.outputShape = [];
- var batch = inputShape[0], oldHeight = inputShape[1], oldWidth = inputShape[2], depth = inputShape[3];
- this.outputShape = [batch, newHeight, newWidth, depth];
- var effectiveInSize = alignCorners ? [oldHeight - 1, oldWidth - 1] : [oldHeight, oldWidth];
- var effectiveOutSize = alignCorners ? [newHeight - 1, newWidth - 1] : [newHeight, newWidth];
- var roundBase = alignCorners ? '0.5' : '0.0';
- this.userCode = "\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n " + effectiveInSize[0] / effectiveOutSize[0] + ",\n " + effectiveInSize[1] / effectiveOutSize[1] + ");\n const vec2 inputShapeRC = vec2(" + oldHeight + ".0, " + oldWidth + ".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 + " + roundBase + ")));\n\n float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);\n\n setOutput(newValue);\n }\n ";
- }
- return ResizeNearestNeighborProgram;
- }());
-
- var ReverseProgram = (function () {
- function ReverseProgram(xShape, axis) {
- this.variableNames = ['x'];
- var rank = xShape.length;
- if (rank > 4) {
- throw new Error("WebGL backend: Reverse of rank-" + rank + " tensor is not yet supported");
- }
- this.outputShape = xShape;
- if (rank === 1) {
- this.userCode = "\n void main() {\n int coord = getOutputCoords();\n setOutput(getX(" + xShape[0] + " - coord - 1));\n }\n ";
- return;
- }
- var getInCoord = function (i) {
- if (axis.indexOf(i) !== -1 && xShape[i] !== 1) {
- return xShape[i] + " - coords[" + i + "] - 1";
- }
- return "coords[" + i + "]";
- };
- var inCoords = xShape.map(function (_, i) { return getInCoord(i); }).join(',');
- var type = getCoordsDataType(rank);
- this.userCode = "\n void main() {\n " + type + " coords = getOutputCoords();\n setOutput(getX(" + inCoords + "));\n }\n ";
- }
- return ReverseProgram;
- }());
-
- var SliceProgram = (function () {
- function SliceProgram(destSize) {
- this.variableNames = ['source'];
- this.outputShape = destSize;
- this.rank = destSize.length;
- var dtype = getCoordsDataType(this.rank);
- var sourceCoords = getCoords$1(this.rank);
- this.userCode = "\n uniform " + dtype + " start;\n\n void main() {\n " + dtype + " sourceLoc = start + getOutputCoords();\n setOutput(getSource(" + sourceCoords + "));\n }\n ";
- }
- SliceProgram.prototype.getCustomSetupFunc = function (start) {
- var _this = this;
- if (start.length !== this.rank) {
- throw Error("The rank (" + this.rank + ") of the program must match the " +
- ("length of start (" + start.length + ")"));
- }
- return function (gpgpu, webGLProgram) {
- if (_this.startLoc == null) {
- _this.startLoc = gpgpu.getUniformLocationNoThrow(webGLProgram, 'start');
- if (_this.startLoc == null) {
- return;
- }
- }
- if (_this.rank === 1) {
- gpgpu.gl.uniform1i(_this.startLoc, start[0]);
- }
- else if (_this.rank === 2) {
- gpgpu.gl.uniform2i(_this.startLoc, start[0], start[1]);
- }
- else if (_this.rank === 3) {
- gpgpu.gl.uniform3i(_this.startLoc, start[0], start[1], start[2]);
- }
- else if (_this.rank === 4) {
- gpgpu.gl.uniform4i(_this.startLoc, start[0], start[1], start[2], start[3]);
- }
- else {
- throw Error("Slicing for rank " + _this.rank + " is not yet supported");
- }
- };
- };
- return SliceProgram;
- }());
- function getCoords$1(rank) {
- if (rank === 1) {
- return 'sourceLoc';
- }
- else if (rank === 2) {
- return 'sourceLoc.x, sourceLoc.y';
- }
- else if (rank === 3) {
- return 'sourceLoc.x, sourceLoc.y, sourceLoc.z';
- }
- else if (rank === 4) {
- return 'sourceLoc.x, sourceLoc.y, sourceLoc.z, sourceLoc.w';
- }
- else {
- throw Error("Slicing for rank " + rank + " is not yet supported");
- }
- }
-
- var StridedSliceProgram = (function () {
- function StridedSliceProgram(begin, strides, shape) {
- this.variableNames = ['x'];
- this.outputShape = shape;
- this.rank = shape.length;
- var dtype = getCoordsDataType(this.rank);
- var newCoords = '';
- if (this.rank === 1) {
- newCoords = 'coords * strides + begin';
- }
- else {
- newCoords =
- shape.map(function (_, i) { return "coords[" + i + "] * strides[" + i + "] + begin[" + i + "]"; })
- .join(',');
- }
- this.userCode = "\n " + dtype + " begin = " + dtype + "(" + begin + ");\n " + dtype + " strides = " + dtype + "(" + strides + ");\n\n void main() {\n " + dtype + " coords = getOutputCoords();\n setOutput(getX(" + newCoords + "));\n }\n ";
- }
- return StridedSliceProgram;
- }());
-
- var TextureManager = (function () {
- function TextureManager(gpgpu) {
- this.gpgpu = gpgpu;
- this.numUsedTextures = 0;
- this.numFreeTextures = 0;
- this.freeTextures = {};
- this.logEnabled = false;
- this.allocatedTextures = [];
- this.usedTextureCount = {};
- }
- TextureManager.prototype.acquireTexture = function (shapeRC, texType) {
- if (texType === void 0) { texType = TextureType.FLOAT; }
- var shapeKey = getKeyFromTextureShape(shapeRC, texType);
- if (!(shapeKey in this.freeTextures)) {
- this.freeTextures[shapeKey] = [];
- }
- if (!(shapeKey in this.usedTextureCount)) {
- this.usedTextureCount[shapeKey] = 0;
- }
- this.usedTextureCount[shapeKey]++;
- if (this.freeTextures[shapeKey].length > 0) {
- this.numFreeTextures--;
- this.numUsedTextures++;
- this.log();
- return this.freeTextures[shapeKey].shift();
- }
- this.numUsedTextures++;
- this.log();
- var newTexture = this.gpgpu.createMatrixTexture(shapeRC[0], shapeRC[1]);
- this.allocatedTextures.push(newTexture);
- return newTexture;
- };
- TextureManager.prototype.releaseTexture = function (texture, shape, texType) {
- if (texType === void 0) { texType = TextureType.FLOAT; }
- var shapeKey = getKeyFromTextureShape(shape, texType);
- if (!(shapeKey in this.freeTextures)) {
- this.freeTextures[shapeKey] = [];
- }
- this.freeTextures[shapeKey].push(texture);
- this.numFreeTextures++;
- this.numUsedTextures--;
- this.usedTextureCount[shapeKey]--;
- this.log();
- };
- TextureManager.prototype.log = function () {
- if (!this.logEnabled) {
- return;
- }
- var total = this.numFreeTextures + this.numUsedTextures;
- console.log('Free/Used', this.numFreeTextures + " / " + this.numUsedTextures, "(" + total + ")");
- };
- TextureManager.prototype.getNumUsedTextures = function () {
- return this.numUsedTextures;
- };
- TextureManager.prototype.getNumFreeTextures = function () {
- return this.numFreeTextures;
- };
- TextureManager.prototype.dispose = function () {
- var _this = this;
- if (this.allocatedTextures == null) {
- return;
- }
- this.allocatedTextures.forEach(function (texture) {
- _this.gpgpu.deleteMatrixTexture(texture);
- });
- this.freeTextures = null;
- this.allocatedTextures = null;
- this.usedTextureCount = null;
- this.numUsedTextures = 0;
- this.numFreeTextures = 0;
- };
- return TextureManager;
- }());
- function getKeyFromTextureShape(shapeRowsCol, texType) {
- return shapeRowsCol[0] + "_" + shapeRowsCol[1] + "_" + texType;
- }
-
- var TileProgram = (function () {
- function TileProgram(aShape, reps) {
- this.variableNames = ['A'];
- var outputShape = new Array(aShape.length);
- for (var i = 0; i < outputShape.length; i++) {
- outputShape[i] = aShape[i] * reps[i];
- }
- this.outputShape = outputShape;
- this.rank = outputShape.length;
- var dtype = getCoordsDataType(this.rank);
- var sourceCoords = getSourceCoords$1(aShape);
- this.userCode = "\n void main() {\n " + dtype + " resRC = getOutputCoords();\n setOutput(getA(" + sourceCoords + "));\n }\n ";
- }
- return TileProgram;
- }());
- function getSourceCoords$1(aShape) {
- var rank = aShape.length;
- if (rank > 4) {
- throw Error("Tile for rank " + rank + " is not yet supported");
- }
- if (rank === 1) {
- return "imod(resRC, " + aShape[0] + ")";
- }
- var currentCoords = ['resRC.x', 'resRC.y', 'resRC.z', 'resRC.w'];
- var sourceCoords = [];
- for (var i = 0; i < aShape.length; i++) {
- sourceCoords.push("imod(" + currentCoords[i] + ", " + aShape[i] + ")");
- }
- return sourceCoords.join();
- }
-
- var TransposeProgram = (function () {
- function TransposeProgram(aShape, newDim) {
- this.variableNames = ['A'];
- var outputShape = new Array(aShape.length);
- for (var i = 0; i < outputShape.length; i++) {
- outputShape[i] = aShape[newDim[i]];
- }
- this.outputShape = outputShape;
- this.rank = outputShape.length;
- var dtype = getCoordsDataType(this.rank);
- var switched = getSwitchedCoords(newDim);
- this.userCode = "\n void main() {\n " + dtype + " resRC = getOutputCoords();\n setOutput(getA(" + switched + "));\n }\n ";
- }
- return TransposeProgram;
- }());
- function getSwitchedCoords(newDim) {
- var rank = newDim.length;
- if (rank > 4) {
- throw Error("Transpose for rank " + rank + " is not yet supported");
- }
- var originalOrder = ['resRC.x', 'resRC.y', 'resRC.z', 'resRC.w'];
- var switchedCoords = new Array(rank);
- for (var i = 0; i < newDim.length; i++) {
- switchedCoords[newDim[i]] = originalOrder[i];
- }
- return switchedCoords.join();
- }
-
- var ERF_P = 0.3275911;
- var ERF_A1 = 0.254829592;
- var ERF_A2 = -0.284496736;
- var ERF_A3 = 1.421413741;
- var ERF_A4 = -1.453152027;
- var ERF_A5 = 1.061405429;
-
- var UnaryOpProgram = (function () {
- function UnaryOpProgram(aShape, opSnippet) {
- this.variableNames = ['A'];
- this.outputShape = aShape;
- this.userCode = "\n float unaryOperation(float x) {\n " + opSnippet + "\n }\n\n void main() {\n float x = getAAtOutCoords();\n float y = unaryOperation(x);\n\n setOutput(y);\n }\n ";
- }
- return UnaryOpProgram;
- }());
- var CHECK_NAN_SNIPPET$1 = "if (isNaN(x)) return x;";
- var ABS = "return abs(x);";
- var RELU = CHECK_NAN_SNIPPET$1 + "\n return (x < 0.0) ? 0.0 : x;\n";
- var ELU = "return (x >= 0.0) ? x : (exp(x) - 1.0);";
- var SELU = "\n // Stable and Attracting Fixed Point (0, 1) for Normalized Weights.\n // see: https://arxiv.org/abs/1706.02515\n float scaleAlpha = " + SELU_SCALEALPHA + ";\n float scale = " + SELU_SCALE + ";\n return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);\n";
- function STEP(alpha) {
- if (alpha === void 0) { alpha = 0.0; }
- return CHECK_NAN_SNIPPET$1 + ("\n return x > 0.0 ? 1.0 : float(" + alpha + ");\n ");
- }
- var NEG = "return -x;";
- var CEIL = "return ceil(x);";
- var FLOOR = "return floor(x);";
- var SIGN = "\n if (isNaN(x)) { return 0.0; }\n return sign(x);\n";
- var ROUND = "\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";
- var EXP = "return exp(x);";
- var EXPM1 = "return exp(x) - 1.0;";
- var LOG = "return log(x);";
- var LOG1P = "return log(1.0 + x);";
- var SQRT = "return sqrt(x);";
- var RSQRT = "return inversesqrt(x);";
- var SIGMOID = "return 1.0 / (1.0 + exp(-1.0 * x));";
- var SOFTPLUS = "\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";
- var SIN = "return sin(x);";
- var COS = "return cos(x);";
- var TAN = "return tan(x);";
- var ASIN = "return asin(x);";
- var ACOS = "return acos(x);";
- var ATAN = CHECK_NAN_SNIPPET$1 + "\n return atan(x);\n";
- var SINH = "\n float e2x = exp(x);\n return (e2x - 1.0 / e2x) / 2.0;\n";
- var COSH = "\n float e2x = exp(-x);\n return (e2x + 1.0 / e2x) / 2.0;\n";
- var TANH = "\n float e2x = exp(-2.0 * abs(x));\n return sign(x) * (1.0 - e2x) / (1.0 + e2x);\n";
- var ASINH = "return log(x + sqrt(x * x + 1.0));";
- var ACOSH = "return log(x + sqrt(x * x - 1.0));";
- var ATANH = "return (log(1.0 + x) - log(1.0 - x)) / 2.0;";
- var ERF = "\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 = " + ERF_P + ";\n float a1 = " + ERF_A1 + ";\n float a2 = " + ERF_A2 + ";\n float a3 = " + ERF_A3 + ";\n float a4 = " + ERF_A4 + ";\n float a5 = " + ERF_A5 + ";\n\n float t = 1.0 / (1.0 + p * x);\n return 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);\n";
- var SQUARE = "return x * x;";
- var RECIPROCAL = "return 1.0 / x;";
- var LOGICAL_NOT = "return float(!(x >= 1.0));";
- var TO_INT = "return float(int(x));";
-
- var __awaiter$5 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step$$1(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step$$1(generator["throw"](value)); } catch (e) { reject(e); } }
- function step$$1(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step$$1((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$5 = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step$$1([n, v]); }; }
- function step$$1(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var MathBackendWebGL = (function () {
- function MathBackendWebGL(gpgpu, delayedStorage) {
- if (delayedStorage === void 0) { delayedStorage = true; }
- this.gpgpu = gpgpu;
- this.delayedStorage = delayedStorage;
- this.texData = new WeakMap();
- this.uploadWaitMs = 0;
- this.downloadWaitMs = 0;
- this.binaryCache = {};
- this.disposed = false;
- if (ENV.get('WEBGL_VERSION') < 1) {
- throw new Error('WebGL is not supported on this device');
- }
- if (typeof document !== 'undefined') {
- this.canvas = document.createElement('canvas');
- }
- if (gpgpu == null) {
- this.gpgpu = new GPGPUContext(createWebGLContext(this.canvas));
- this.gpgpuCreatedLocally = true;
- }
- else {
- this.gpgpuCreatedLocally = false;
- }
- this.textureManager = new TextureManager(this.gpgpu);
- }
- MathBackendWebGL.prototype.register = function (dataId, shape, dtype) {
- if (this.texData.has(dataId)) {
- throw new Error('Data buffer is already registered');
- }
- this.texData.set(dataId, {
- shape: shape,
- dtype: dtype,
- values: null,
- texture: null,
- texShape: null,
- texType: TextureType.FLOAT
- });
- };
- MathBackendWebGL.prototype.fromPixels = function (pixels, numChannels) {
- if (pixels == null) {
- throw new Error('MathBackendWebGL.writePixels(): pixels can not be null');
- }
- var texShape = [pixels.height, pixels.width];
- var outShape = [pixels.height, pixels.width, numChannels];
- if (pixels instanceof HTMLVideoElement) {
- if (this.canvas == null) {
- throw new Error('Can\'t read pixels from HTMLImageElement outside ' +
- 'the browser.');
- }
- this.canvas.width = pixels.width;
- this.canvas.height = pixels.height;
- this.canvas.getContext('2d').drawImage(pixels, 0, 0, pixels.width, pixels.height);
- pixels = this.canvas;
- }
- var tempPixelArray = Tensor.make(texShape, {}, 'int32');
- this.texData.get(tempPixelArray.dataId).texType = TextureType.UNSIGNED_BYTE;
- this.gpgpu.uploadPixelDataToTexture(this.getTexture(tempPixelArray.dataId), pixels);
- var program = new FromPixelsProgram(outShape);
- var res = this.compileAndRun(program, [tempPixelArray]);
- tempPixelArray.dispose();
- return res;
- };
- MathBackendWebGL.prototype.write = function (dataId, values) {
- if (values == null) {
- throw new Error('MathBackendWebGL.write(): values can not be null');
- }
- this.throwIfNoData(dataId);
- var texData = this.texData.get(dataId);
- var texture = texData.texture, texShape = texData.texShape, texType = texData.texType;
- if (texture != null) {
- this.textureManager.releaseTexture(texture, texShape, texType);
- texData.texture = null;
- texData.texShape = null;
- }
- texData.values = values;
- if (!this.delayedStorage) {
- this.uploadToGPU(dataId);
- }
- };
- MathBackendWebGL.prototype.readSync = function (dataId) {
- this.throwIfNoData(dataId);
- var texData = this.texData.get(dataId);
- var texture = texData.texture, values = texData.values, texShape = texData.texShape;
- if (values != null) {
- this.cacheOnCPU(dataId);
- return values;
- }
- var shouldTimeProgram = this.activeTimers != null;
- var start;
- if (shouldTimeProgram) {
- start = performance.now();
- }
- var float32Values = this.gpgpu.downloadMatrixFromTexture(texture, texShape[0], texShape[1]);
- if (shouldTimeProgram) {
- this.downloadWaitMs += performance.now() - start;
- }
- this.cacheOnCPU(dataId, float32Values);
- return texData.values;
- };
- MathBackendWebGL.prototype.read = function (dataId) {
- return __awaiter$5(this, void 0, void 0, function () {
- var texData, texture, values, texShape, float32Values;
- return __generator$5(this, function (_a) {
- switch (_a.label) {
- case 0:
- this.throwIfNoData(dataId);
- texData = this.texData.get(dataId);
- texture = texData.texture, values = texData.values, texShape = texData.texShape;
- if (values != null) {
- this.cacheOnCPU(dataId);
- return [2, values];
- }
- if (!ENV.get('WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED')) return [3, 2];
- return [4, this.gpgpu.downloadMatrixFromTextureAsync(texture, texShape[0], texShape[1])];
- case 1:
- float32Values = _a.sent();
- this.cacheOnCPU(dataId, float32Values);
- return [2, texData.values];
- case 2:
- if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION') === 0) {
- return [2, this.readSync(dataId)];
- }
- return [4, this.gpgpu.runQuery(function () { })];
- case 3:
- _a.sent();
- return [2, this.readSync(dataId)];
- }
- });
- });
- };
- MathBackendWebGL.prototype.time = function (f) {
- return __awaiter$5(this, void 0, void 0, function () {
- var oldActiveTimers, newActiveTimers, outerMostTime, flattenedActiveTimers, kernelMs, res;
- return __generator$5(this, function (_a) {
- switch (_a.label) {
- case 0:
- oldActiveTimers = this.activeTimers;
- newActiveTimers = [];
- outerMostTime = false;
- if (this.programTimersStack == null) {
- this.programTimersStack = newActiveTimers;
- outerMostTime = true;
- }
- else {
- this.activeTimers.push(newActiveTimers);
- }
- this.activeTimers = newActiveTimers;
- f();
- flattenedActiveTimers = flatten(this.activeTimers);
- this.activeTimers = oldActiveTimers;
- if (outerMostTime) {
- this.programTimersStack = null;
- }
- return [4, Promise.all(flattenedActiveTimers).then(function (results) {
- var sum$$1 = 0;
- results.forEach(function (result) { return sum$$1 += result; });
- return sum$$1;
- })];
- case 1:
- kernelMs = _a.sent();
- res = {
- uploadWaitMs: this.uploadWaitMs,
- downloadWaitMs: this.downloadWaitMs,
- kernelMs: kernelMs,
- wallMs: null
- };
- this.uploadWaitMs = 0;
- this.downloadWaitMs = 0;
- return [2, res];
- }
- });
- });
- };
- MathBackendWebGL.prototype.memory = function () {
- return { unreliable: false };
- };
- MathBackendWebGL.prototype.startTimer = function () {
- if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION') > 0) {
- return this.gpgpu.beginQuery();
- }
- return { startMs: performance.now(), endMs: null };
- };
- MathBackendWebGL.prototype.endTimer = function (query) {
- if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION') > 0) {
- this.gpgpu.endQuery();
- return query;
- }
- query.endMs = performance.now();
- return query;
- };
- MathBackendWebGL.prototype.getQueryTime = function (query) {
- return __awaiter$5(this, void 0, void 0, function () {
- var timerQuery;
- return __generator$5(this, function (_a) {
- if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION') > 0) {
- return [2, this.gpgpu.pollQueryTime(query)];
- }
- timerQuery = query;
- return [2, timerQuery.endMs - timerQuery.startMs];
- });
- });
- };
- MathBackendWebGL.prototype.disposeData = function (dataId) {
- if (this.texData.has(dataId)) {
- var _a = this.texData.get(dataId), texture = _a.texture, texShape = _a.texShape, texType = _a.texType;
- if (texture != null) {
- this.textureManager.releaseTexture(texture, texShape, texType);
- }
- this.texData.delete(dataId);
- }
- };
- MathBackendWebGL.prototype.getTexture = function (dataId) {
- this.uploadToGPU(dataId);
- return this.texData.get(dataId).texture;
- };
- MathBackendWebGL.prototype.getTextureData = function (dataId) {
- this.uploadToGPU(dataId);
- return this.texData.get(dataId);
- };
- MathBackendWebGL.prototype.getGPGPUContext = function () {
- return this.gpgpu;
- };
- MathBackendWebGL.prototype.getCanvas = function () {
- return this.canvas;
- };
- MathBackendWebGL.prototype.slice = function (x, begin, size) {
- var program = new SliceProgram(size);
- var customSetup = program.getCustomSetupFunc(begin);
- return this.compileAndRun(program, [x], null, customSetup);
- };
- MathBackendWebGL.prototype.stridedSlice = function (x, begin, end, strides, beginMask, endMask) {
- var _a = getStridedSlicedInfo(x.shape, begin, end, strides, beginMask, endMask), beginIndex = _a[0], size = _a[1];
- if (size.some(function (axis) { return axis === 0; })) {
- return tensor([], size);
- }
- var program = new StridedSliceProgram(beginIndex, strides, size);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.reverse = function (x, axis) {
- var program = new ReverseProgram(x.shape, axis);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.concat = function (a, b) {
- var program = new ConcatProgram(a.shape, b.shape);
- return this.compileAndRun(program, [a, b]);
- };
- MathBackendWebGL.prototype.neg = function (x) {
- var program = new UnaryOpProgram(x.shape, NEG);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.matMul = function (a, b, transposeA, transposeB) {
- var program = new MatMulProgram(a.shape, b.shape, transposeA, transposeB);
- return this.compileAndRun(program, [a, b]);
- };
- MathBackendWebGL.prototype.multiply = function (a, b) {
- var program = new BinaryOpProgram(MUL, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, upcastType(a.dtype, b.dtype));
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.batchNormalization = function (x, mean$$1, variance, varianceEpsilon, scale, offset) {
- var inputs = [x, mean$$1, variance];
- var offsetShape = null;
- if (offset != null) {
- offsetShape = offset.shape;
- inputs.push(offset);
- }
- var scaleShape = null;
- if (scale != null) {
- scaleShape = scale.shape;
- inputs.push(scale);
- }
- var program = new BatchNormProgram(x.shape, mean$$1.shape, variance.shape, offsetShape, scaleShape, varianceEpsilon);
- return this.compileAndRun(program, inputs);
- };
- MathBackendWebGL.prototype.localResponseNormalization4D = function (x, radius, bias, alpha, beta) {
- var program = new LRNProgram(x.shape, radius, bias, alpha, beta);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.tile = function (x, reps) {
- var program = new TileProgram(x.shape, reps);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.pad = function (x, paddings, constantValue) {
- var program = new PadProgram(x.shape, paddings, constantValue);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.transpose = function (x, perm) {
- var program = new TransposeProgram(x.shape, perm);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.gather = function (x, indices, axis) {
- var program = new GatherProgram(x.shape, indices.size, axis);
- return this.compileAndRun(program, [x, indices]);
- };
- MathBackendWebGL.prototype.reduce = function (x, reduceType, dtype) {
- var batchSize = x.shape[0];
- var inSize = x.shape[1];
- var windowSize = computeOptimalWindowSize(inSize);
- var reduceInfo = { windowSize: windowSize, inSize: inSize, batchSize: batchSize };
- var program = new ReduceProgram(reduceInfo, reduceType);
- var _a = program.outputShape, rows = _a[0], cols = _a[1];
- var output = this.makeOutputArray([rows, cols], dtype);
- this.compileAndRun(program, [x], output);
- if (output.shape[1] === 1) {
- return output;
- }
- return this.reduce(output, reduceType, dtype);
- };
- MathBackendWebGL.prototype.argReduce = function (x, reduceType, bestIndicesA) {
- if (bestIndicesA === void 0) { bestIndicesA = null; }
- var batchSize = x.shape[0];
- var inSize = x.shape[1];
- if (bestIndicesA != null) {
- batchSize = bestIndicesA.shape[0];
- inSize = bestIndicesA.shape[1];
- }
- var windowSize = computeOptimalWindowSize(inSize);
- var reduceInfo = { windowSize: windowSize, inSize: inSize, batchSize: batchSize };
- var program = new ArgMinMaxProgram(reduceInfo, reduceType, bestIndicesA == null);
- var _a = program.outputShape, rows = _a[0], cols = _a[1];
- var output = this.makeOutputArray([rows, cols], 'int32');
- var inputs = [x];
- if (bestIndicesA != null) {
- inputs.push(bestIndicesA);
- }
- this.compileAndRun(program, inputs, output);
- if (output.shape[1] === 1) {
- return output;
- }
- return this.argReduce(x, reduceType, output);
- };
- MathBackendWebGL.prototype.sum = function (x, axes) {
- assertAxesAreInnerMostDims('sum', axes, x.rank);
- var _a = computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
- var inSize = sizeFromShape(reduceShape);
- var a2D = x.as2D(-1, inSize);
- var outputDType = sumOutType(x.dtype);
- return this.reduce(a2D, 'sum', outputDType).reshape(outShape);
- };
- MathBackendWebGL.prototype.argMin = function (x, axis) {
- var axes = [axis];
- assertAxesAreInnerMostDims('argMin', axes, x.rank);
- var _a = computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
- var inSize = sizeFromShape(reduceShape);
- var a2D = x.as2D(-1, inSize);
- return this.argReduce(a2D, 'min').reshape(outShape);
- };
- MathBackendWebGL.prototype.argMax = function (x, axis) {
- var axes = [axis];
- assertAxesAreInnerMostDims('argMax', axes, x.rank);
- var _a = computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
- var inSize = sizeFromShape(reduceShape);
- var a2D = x.as2D(-1, inSize);
- return this.argReduce(a2D, 'max').reshape(outShape);
- };
- MathBackendWebGL.prototype.cumsum = function (x, axis, exclusive, reverse$$1) {
- var program = new CumSumProgram(x.shape, exclusive, reverse$$1);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.equal = function (a, b) {
- var program = new BinaryOpProgram(EQUAL, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, 'bool');
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.notEqual = function (a, b) {
- var program = new BinaryOpProgram(NOT_EQUAL, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, 'bool');
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.less = function (a, b) {
- var program = new BinaryOpProgram(LESS, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, 'bool');
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.lessEqual = function (a, b) {
- var program = new BinaryOpProgram(LESS_EQUAL, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, 'bool');
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.greater = function (a, b) {
- var program = new BinaryOpProgram(GREATER, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, 'bool');
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.greaterEqual = function (a, b) {
- var program = new BinaryOpProgram(GREATER_EQUAL, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, 'bool');
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.logicalNot = function (x) {
- var program = new UnaryOpProgram(x.shape, LOGICAL_NOT);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.logicalAnd = function (a, b) {
- var program = new BinaryOpProgram(LOGICAL_AND, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, 'bool');
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.logicalOr = function (a, b) {
- var program = new BinaryOpProgram(LOGICAL_OR, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, 'bool');
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.where = function (condition, a, b, dtype) {
- var program = new WhereProgram(condition.rank, a.shape, a.rank);
- var output = this.makeOutputArray(program.outputShape, dtype);
- return this.compileAndRun(program, [condition, a, b], output);
- };
- MathBackendWebGL.prototype.topKValues = function (x, k) {
- throw new Error('topKValues GPU not yet implemented!');
- };
- MathBackendWebGL.prototype.topKIndices = function (x, k) {
- throw new Error('topKIndices GPU not yet implemented!');
- };
- MathBackendWebGL.prototype.min = function (x, axes) {
- assertAxesAreInnerMostDims('min', axes, x.rank);
- var _a = computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
- var inSize = sizeFromShape(reduceShape);
- var a2D = x.as2D(-1, inSize);
- return this.reduce(a2D, 'min', a2D.dtype).reshape(outShape);
- };
- MathBackendWebGL.prototype.minimum = function (a, b) {
- var program = new BinaryOpProgram(MIN, a.shape, b.shape);
- return this.compileAndRun(program, [a, b]);
- };
- MathBackendWebGL.prototype.mod = function (a, b) {
- var program = new BinaryOpProgram(MOD, a.shape, b.shape);
- return this.compileAndRun(program, [a, b]);
- };
- MathBackendWebGL.prototype.max = function (x, axes) {
- assertAxesAreInnerMostDims('max', axes, x.rank);
- var _a = computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
- var inSize = sizeFromShape(reduceShape);
- var a2D = x.as2D(-1, inSize);
- return this.reduce(a2D, 'max', a2D.dtype).reshape(outShape);
- };
- MathBackendWebGL.prototype.maximum = function (a, b) {
- var program = new BinaryOpProgram(MAX, a.shape, b.shape);
- return this.compileAndRun(program, [a, b]);
- };
- MathBackendWebGL.prototype.squaredDifference = function (a, b) {
- var program = new BinaryOpProgram(SQUARED_DIFFERENCE, a.shape, b.shape);
- return this.compileAndRun(program, [a, b]);
- };
- MathBackendWebGL.prototype.divide = function (a, b) {
- var op;
- var outputDtype;
- if (a.dtype === 'int32' && b.dtype === 'int32') {
- op = INT_DIV;
- outputDtype = 'int32';
- }
- else {
- op = DIV;
- outputDtype = 'float32';
- }
- var program = new BinaryOpProgram(op, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, outputDtype);
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.add = function (a, b) {
- var program = new BinaryOpProgram(ADD, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, upcastType(a.dtype, b.dtype));
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.subtract = function (a, b) {
- var program = new BinaryOpProgram(SUB, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, upcastType(a.dtype, b.dtype));
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.pow = function (a, b) {
- var program = new BinaryOpProgram(POW, a.shape, b.shape);
- var output = this.makeOutputArray(program.outputShape, upcastType(a.dtype, b.dtype));
- return this.compileAndRun(program, [a, b], output);
- };
- MathBackendWebGL.prototype.ceil = function (x) {
- var program = new UnaryOpProgram(x.shape, CEIL);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.floor = function (x) {
- var program = new UnaryOpProgram(x.shape, FLOOR);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.sign = function (x) {
- var program = new UnaryOpProgram(x.shape, SIGN);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.round = function (x) {
- var program = new UnaryOpProgram(x.shape, ROUND);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.exp = function (x) {
- var program = new UnaryOpProgram(x.shape, EXP);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.expm1 = function (x) {
- var program = new UnaryOpProgram(x.shape, EXPM1);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.log = function (x) {
- var program = new UnaryOpProgram(x.shape, LOG);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.log1p = function (x) {
- var program = new UnaryOpProgram(x.shape, LOG1P);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.sqrt = function (x) {
- var program = new UnaryOpProgram(x.shape, SQRT);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.rsqrt = function (x) {
- var program = new UnaryOpProgram(x.shape, RSQRT);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.square = function (x) {
- var program = new UnaryOpProgram(x.shape, SQUARE);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.reciprocal = function (x) {
- var program = new UnaryOpProgram(x.shape, RECIPROCAL);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.relu = function (x) {
- var program = new UnaryOpProgram(x.shape, RELU);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.elu = function (x) {
- var program = new UnaryOpProgram(x.shape, ELU);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.eluDer = function (dy, y) {
- var program = new BinaryOpProgram(ELU_DER, dy.shape, y.shape);
- return this.compileAndRun(program, [dy, y]);
- };
- MathBackendWebGL.prototype.selu = function (x) {
- var program = new UnaryOpProgram(x.shape, SELU);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.int = function (x) {
- var program = new UnaryOpProgram(x.shape, TO_INT);
- var output = this.makeOutputArray(program.outputShape, 'int32');
- return this.compileAndRun(program, [x], output);
- };
- MathBackendWebGL.prototype.clip = function (x, min$$1, max$$1) {
- var program = new ClipProgram(x.shape, min$$1, max$$1);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.abs = function (x) {
- var program = new UnaryOpProgram(x.shape, ABS);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.sigmoid = function (x) {
- var program = new UnaryOpProgram(x.shape, SIGMOID);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.softplus = function (x) {
- var program = new UnaryOpProgram(x.shape, SOFTPLUS);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.sin = function (x) {
- var program = new UnaryOpProgram(x.shape, SIN);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.cos = function (x) {
- var program = new UnaryOpProgram(x.shape, COS);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.tan = function (x) {
- var program = new UnaryOpProgram(x.shape, TAN);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.asin = function (x) {
- var program = new UnaryOpProgram(x.shape, ASIN);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.acos = function (x) {
- var program = new UnaryOpProgram(x.shape, ACOS);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.atan = function (x) {
- var program = new UnaryOpProgram(x.shape, ATAN);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.atan2 = function (a, b) {
- var program = new BinaryOpProgram(ATAN2, a.shape, b.shape);
- return this.compileAndRun(program, [a, b]);
- };
- MathBackendWebGL.prototype.sinh = function (x) {
- var program = new UnaryOpProgram(x.shape, SINH);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.cosh = function (x) {
- var program = new UnaryOpProgram(x.shape, COSH);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.tanh = function (x) {
- var program = new UnaryOpProgram(x.shape, TANH);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.asinh = function (x) {
- var program = new UnaryOpProgram(x.shape, ASINH);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.acosh = function (x) {
- var program = new UnaryOpProgram(x.shape, ACOSH);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.atanh = function (x) {
- var program = new UnaryOpProgram(x.shape, ATANH);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.erf = function (x) {
- var program = new UnaryOpProgram(x.shape, ERF);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.step = function (x, alpha) {
- var program = new UnaryOpProgram(x.shape, STEP(alpha));
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.conv2d = function (x, filter, convInfo) {
- var program = new Conv2DProgram(convInfo);
- return this.compileAndRun(program, [x, filter]);
- };
- MathBackendWebGL.prototype.conv2dDerInput = function (dy, filter, convInfo) {
- var program = new Conv2DDerInputProgram(convInfo);
- return this.compileAndRun(program, [dy, filter]);
- };
- MathBackendWebGL.prototype.conv2dDerFilter = function (x, dy, convInfo) {
- var program = new Conv2DDerFilterProgram(convInfo);
- return this.compileAndRun(program, [x, dy]);
- };
- MathBackendWebGL.prototype.depthwiseConv2D = function (x, filter, convInfo) {
- var program = new DepthwiseConv2DProgram(convInfo);
- return this.compileAndRun(program, [x, filter]);
- };
- MathBackendWebGL.prototype.maxPool = function (x, convInfo) {
- var program = new Pool2DProgram(convInfo, 'max', false);
- var output = this.makeOutputArray(program.outputShape, x.dtype);
- return this.compileAndRun(program, [x], output);
- };
- MathBackendWebGL.prototype.avgPool = function (x, convInfo) {
- var program = new Pool2DProgram(convInfo, 'avg', false);
- var output = this.makeOutputArray(program.outputShape, 'float32');
- return this.compileAndRun(program, [x], output);
- };
- MathBackendWebGL.prototype.maxPoolBackprop = function (dy, x, y, convInfo) {
- var getPositions = true;
- var maxPoolPositionsProgram = new Pool2DProgram(convInfo, 'max', getPositions);
- var maxPoolPositions = this.compileAndRun(maxPoolPositionsProgram, [x]);
- var maxPoolBackPropProgram = new MaxPool2DBackpropProgram(convInfo);
- var output = this.makeOutputArray(maxPoolBackPropProgram.outputShape, x.dtype);
- var result = this.compileAndRun(maxPoolBackPropProgram, [dy, maxPoolPositions], output);
- maxPoolPositions.dispose();
- return result;
- };
- MathBackendWebGL.prototype.avgPoolBackprop = function (dy, x, convInfo) {
- var avgPoolBackpropProgram = new AvgPool2DBackpropProgram(convInfo);
- var output = this.makeOutputArray(avgPoolBackpropProgram.outputShape, x.dtype);
- return this.compileAndRun(avgPoolBackpropProgram, [dy], output);
- };
- MathBackendWebGL.prototype.cast = function (x, dtype) {
- return castTensor(x, dtype, this);
- };
- MathBackendWebGL.prototype.reshape = function (x, shape) {
- return reshapeTensor(x, shape);
- };
- MathBackendWebGL.prototype.resizeBilinear = function (x, newHeight, newWidth, alignCorners) {
- var program = new ResizeBilinearProgram(x.shape, newHeight, newWidth, alignCorners);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.resizeBilinearBackprop = function (dy, x, alignCorners) {
- var program = new ResizeBilinearBackpropProgram(dy, x, alignCorners);
- return this.compileAndRun(program, [dy]);
- };
- MathBackendWebGL.prototype.resizeNearestNeighbor = function (x, newHeight, newWidth, alignCorners) {
- var program = new ResizeNearestNeighborProgram(x.shape, newHeight, newWidth, alignCorners);
- return this.compileAndRun(program, [x]);
- };
- MathBackendWebGL.prototype.multinomial = function (logits, normalized, numSamples, seed) {
- var probs = normalized ? logits : softmax(logits);
- var batchSize = probs.shape[0];
- var numOutcomes = probs.shape[1];
- var program = new MultinomialProgram(batchSize, numOutcomes, numSamples);
- var output = this.makeOutputArray(program.outputShape, 'int32');
- var customSetup = program.getCustomSetupFunc(seed);
- return this.compileAndRun(program, [probs], output, customSetup);
- };
- MathBackendWebGL.prototype.oneHot = function (indices, depth, onValue, offValue) {
- var program = new OneHotProgram(indices.size, depth, onValue, offValue);
- return this.compileAndRun(program, [indices]);
- };
- MathBackendWebGL.prototype.makeOutputArray = function (shape, dtype) {
- return Tensor.make(shape, {}, dtype);
- };
- MathBackendWebGL.prototype.compileAndRun = function (program, inputs, output, customSetup) {
- var _this = this;
- if (output == null) {
- output = this.makeOutputArray(program.outputShape, inputs[0].dtype);
- }
- var inputsData = inputs.map(function (input) {
- _this.uploadToGPU(input.dataId);
- return { tensor: input, texData: _this.texData.get(input.dataId) };
- });
- this.uploadToGPU(output.dataId);
- var outputData = {
- tensor: output,
- texData: this.texData.get(output.dataId)
- };
- var key = makeShaderKey(program, inputsData, outputData);
- var binary = this.getAndSaveBinary(key, function () {
- return compileProgram(_this.gpgpu, program, inputsData, outputData);
- });
- var shouldTimeProgram = this.activeTimers != null;
- var query;
- if (shouldTimeProgram) {
- query = this.startTimer();
- }
- runProgram(binary, inputsData, outputData, customSetup);
- if (shouldTimeProgram) {
- query = this.endTimer(query);
- this.activeTimers.push(this.getQueryTime(query));
- }
- return output;
- };
- MathBackendWebGL.prototype.getAndSaveBinary = function (key, getBinary) {
- if (!(key in this.binaryCache)) {
- this.binaryCache[key] = getBinary();
- }
- return this.binaryCache[key];
- };
- MathBackendWebGL.prototype.getTextureManager = function () {
- return this.textureManager;
- };
- MathBackendWebGL.prototype.dispose = function () {
- if (this.disposed) {
- return;
- }
- for (var key in this.binaryCache) {
- this.gpgpu.deleteProgram(this.binaryCache[key].webGLProgram);
- }
- this.textureManager.dispose();
- this.canvas.remove();
- if (this.gpgpuCreatedLocally) {
- this.gpgpu.dispose();
- }
- this.disposed = true;
- };
- MathBackendWebGL.prototype.throwIfNoData = function (dataId) {
- if (!this.texData.has(dataId)) {
- throw new Error("WebGL backend: No data found for this tensor. " +
- "Did you change your backend in the middle of the program? " +
- "New backends can't use Tensors created with previous backends");
- }
- };
- MathBackendWebGL.prototype.uploadToGPU = function (dataId) {
- this.throwIfNoData(dataId);
- var texData = this.texData.get(dataId);
- var shape = texData.shape, values = texData.values, texture = texData.texture, dtype = texData.dtype, texType = texData.texType;
- if (texture != null) {
- return;
- }
- var shouldTimeProgram = this.activeTimers != null;
- var start;
- if (shouldTimeProgram) {
- start = performance.now();
- }
- var texShape = getTextureShapeFromLogicalShape(this.gpgpu.gl, shape);
- texData.texShape = texShape;
- var newTexture = this.textureManager.acquireTexture(texShape, texType);
- texData.texture = newTexture;
- if (values != null) {
- this.gpgpu.uploadMatrixToTexture(newTexture, texShape[0], texShape[1], typedArrayToFloat32(values, dtype));
- texData.values = null;
- if (shouldTimeProgram) {
- this.uploadWaitMs += performance.now() - start;
- }
- }
- };
- MathBackendWebGL.prototype.cacheOnCPU = function (dataId, float32Values) {
- var dontKeepCopyOnGPU = this.delayedStorage;
- var texData = this.texData.get(dataId);
- var texture = texData.texture, texShape = texData.texShape, dtype = texData.dtype, texType = texData.texType;
- if (dontKeepCopyOnGPU && texture != null) {
- this.textureManager.releaseTexture(texture, texShape, texType);
- texData.texture = null;
- texData.texShape = null;
- }
- if (float32Values != null) {
- texData.values = float32ToTypedArray(float32Values, dtype);
- }
- };
- return MathBackendWebGL;
- }());
- ENV.registerBackend('webgl', function () { return new MathBackendWebGL(); }, 2);
- function float32ToTypedArray(a, dtype) {
- if (dtype === 'float32') {
- return a;
- }
- else if (dtype === 'int32' || dtype === 'bool') {
- var result = (dtype === 'int32') ? new Int32Array(a.length) :
- new Uint8Array(a.length);
- for (var i = 0; i < result.length; ++i) {
- result[i] = Math.round(a[i]);
- }
- return result;
- }
- else {
- throw new Error("Unknown dtype " + dtype);
- }
- }
- function typedArrayToFloat32(a, dtype) {
- return (a instanceof Float32Array) ? a : new Float32Array(a);
- }
-
- var __awaiter$6 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step$$1(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step$$1(generator["throw"](value)); } catch (e) { reject(e); } }
- function step$$1(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step$$1((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$6 = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step$$1([n, v]); }; }
- function step$$1(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var MathBackendCPU = (function () {
- function MathBackendCPU() {
- this.data = new WeakMap();
- if (typeof document !== 'undefined') {
- this.canvas = document.createElement('canvas');
- }
- }
- MathBackendCPU.prototype.register = function (dataId, shape, dtype) {
- if (this.data.has(dataId)) {
- throw new Error("Data buffer is already registered");
- }
- this.data.set(dataId, null);
- };
- MathBackendCPU.prototype.write = function (dataId, values) {
- if (values == null) {
- throw new Error('MathBackendCPU.write(): values can not be null');
- }
- this.throwIfNoData(dataId);
- this.data.set(dataId, values);
- };
- MathBackendCPU.prototype.fromPixels = function (pixels, numChannels) {
- if (pixels == null) {
- throw new Error('MathBackendCPU.writePixels(): pixels can not be null');
- }
- var vals;
- if (pixels instanceof ImageData) {
- vals = pixels.data;
- }
- else if (pixels instanceof HTMLCanvasElement) {
- vals = pixels.getContext('2d')
- .getImageData(0, 0, pixels.width, pixels.height)
- .data;
- }
- else if (pixels instanceof HTMLImageElement ||
- pixels instanceof HTMLVideoElement) {
- if (this.canvas == null) {
- throw new Error('Can\'t read pixels from HTMLImageElement outside ' +
- 'the browser.');
- }
- this.canvas.width = pixels.width;
- this.canvas.height = pixels.height;
- this.canvas.getContext('2d').drawImage(pixels, 0, 0, pixels.width, pixels.height);
- vals = this.canvas.getContext('2d')
- .getImageData(0, 0, pixels.width, pixels.height)
- .data;
- }
- else {
- throw new Error("pixels is of unknown type: " + pixels.constructor.name);
- }
- var values;
- if (numChannels === 4) {
- values = new Int32Array(vals);
- }
- else {
- var numPixels = pixels.width * pixels.height;
- values = new Int32Array(numPixels * numChannels);
- for (var i = 0; i < numPixels; i++) {
- for (var channel = 0; channel < numChannels; ++channel) {
- values[i * numChannels + channel] = vals[i * 4 + channel];
- }
- }
- }
- var outShape = [pixels.height, pixels.width, numChannels];
- return tensor3d(values, outShape, 'int32');
- };
- MathBackendCPU.prototype.read = function (dataId) {
- return __awaiter$6(this, void 0, void 0, function () {
- return __generator$6(this, function (_a) {
- return [2, this.readSync(dataId)];
- });
- });
- };
- MathBackendCPU.prototype.readSync = function (dataId) {
- this.throwIfNoData(dataId);
- return this.data.get(dataId);
- };
- MathBackendCPU.prototype.disposeData = function (dataId) {
- if (this.data.has(dataId)) {
- this.data.delete(dataId);
- }
- };
- MathBackendCPU.prototype.time = function (f) {
- return __awaiter$6(this, void 0, void 0, function () {
- var start, kernelMs;
- return __generator$6(this, function (_a) {
- start = performance.now();
- f();
- kernelMs = performance.now() - start;
- return [2, { kernelMs: kernelMs }];
- });
- });
- };
- MathBackendCPU.prototype.memory = function () {
- return {
- unreliable: true
- };
- };
- MathBackendCPU.prototype.throwIfNoData = function (dataId) {
- if (!this.data.has(dataId)) {
- throw new Error("CPU backend: No data found for this tensor. " +
- "Did you change your backend in the middle of the program? " +
- "New backends can't use Tensors created with previous backends");
- }
- };
- MathBackendCPU.prototype.slice = function (x, begin, size) {
- var buffer$$1 = buffer(size, x.dtype);
- for (var i = 0; i < buffer$$1.size; ++i) {
- var loc = buffer$$1.indexToLoc(i);
- var xLoc = loc.map(function (idx, j) { return idx + begin[j]; });
- buffer$$1.set.apply(buffer$$1, [x.get.apply(x, xLoc)].concat(loc));
- }
- return buffer$$1.toTensor();
- };
- MathBackendCPU.prototype.stridedSlice = function (x, begin, end, strides, beginMask, endMask) {
- var _a = getStridedSlicedInfo(x.shape, begin, end, strides, beginMask, endMask), beginIndex = _a[0], size = _a[1];
- if (size.some(function (axis) { return axis === 0; })) {
- return tensor([], size);
- }
- var buffer$$1 = buffer(size, x.dtype);
- for (var i = 0; i < buffer$$1.size; i++) {
- var loc = buffer$$1.indexToLoc(i);
- var newLoc = new Array(loc.length);
- for (var j = 0; j < newLoc.length; j++) {
- newLoc[j] = loc[j] * strides[j] + beginIndex[j];
- }
- buffer$$1.set.apply(buffer$$1, [x.get.apply(x, newLoc)].concat(loc));
- }
- return buffer$$1.toTensor();
- };
- MathBackendCPU.prototype.reverse = function (x, axis) {
- var buffer$$1 = buffer(x.shape, x.dtype);
- var xBuffer = x.buffer();
- var _loop_1 = function (i) {
- var outLoc = buffer$$1.indexToLoc(i);
- var inLoc = outLoc.slice();
- axis.forEach(function (ax) { return inLoc[ax] = x.shape[ax] - 1 - inLoc[ax]; });
- buffer$$1.set.apply(buffer$$1, [xBuffer.get.apply(xBuffer, inLoc)].concat(outLoc));
- };
- for (var i = 0; i < buffer$$1.size; i++) {
- _loop_1(i);
- }
- return buffer$$1.toTensor();
- };
- MathBackendCPU.prototype.concat = function (a, b) {
- var outShape = computeOutShape(a.shape, b.shape, 1);
- var buffer$$1 = buffer(outShape, a.dtype);
- if (a.shape[0] === 1 && b.shape[0] === 1) {
- var aVals = a.dataSync();
- var bVals = b.dataSync();
- var vals = buffer$$1.values;
- vals.set(aVals, 0);
- vals.set(bVals, a.size);
- return buffer$$1.toTensor();
- }
- for (var i = 0; i < outShape[0]; ++i) {
- for (var j = 0; j < a.shape[1]; ++j) {
- buffer$$1.set(a.get(i, j), i, j);
- }
- for (var j = 0; j < b.shape[1]; ++j) {
- buffer$$1.set(b.get(i, j), i, j + a.shape[1]);
- }
- }
- return buffer$$1.toTensor();
- };
- MathBackendCPU.prototype.neg = function (x) {
- return this.multiply(scalar(-1), x);
- };
- MathBackendCPU.prototype.add = function (a, b) {
- return this.broadcastedBinaryOp(a, b, upcastType(a.dtype, b.dtype), function (aValue, bValue) { return aValue + bValue; });
- };
- MathBackendCPU.prototype.subtract = function (a, b) {
- return this.broadcastedBinaryOp(a, b, upcastType(a.dtype, b.dtype), function (aValue, bValue) { return aValue - bValue; });
- };
- MathBackendCPU.prototype.pow = function (a, b) {
- return this.broadcastedBinaryOp(a, b, a.dtype, function (aValue, bValue) { return Math.pow(aValue, bValue); });
- };
- MathBackendCPU.prototype.matMul = function (a, b, transposeA, transposeB) {
- var sharedDim = transposeA ? a.shape[0] : a.shape[1];
- var leftDim = transposeA ? a.shape[1] : a.shape[0];
- var rightDim = transposeB ? b.shape[0] : b.shape[1];
- var aValues = a.dataSync();
- var bValues = b.dataSync();
- var _a = transposeA ? [1, a.strides[0]] : [a.strides[0], 1], aOuterStep = _a[0], aInnerStep = _a[1];
- var _b = transposeB ? [b.strides[0], 1] : [1, b.strides[0]], bOuterStep = _b[0], bInnerStep = _b[1];
- var aOuterEnd = leftDim * aOuterStep;
- var bOuterEnd = rightDim * bOuterStep;
- var result = new Float32Array(leftDim * rightDim);
- var resultIndex = 0;
- for (var aOuter = 0; aOuter < aOuterEnd; aOuter += aOuterStep) {
- for (var bOuter = 0; bOuter < bOuterEnd; bOuter += bOuterStep) {
- var aInner = aOuter;
- var bInner = bOuter;
- var sum$$1 = 0;
- for (var k = 0; k < sharedDim; ++k) {
- sum$$1 += aValues[aInner] * bValues[bInner];
- aInner += aInnerStep;
- bInner += bInnerStep;
- }
- result[resultIndex++] = sum$$1;
- }
- }
- return tensor2d(result, [leftDim, rightDim]);
- };
- MathBackendCPU.prototype.multiply = function (a, b) {
- return this.broadcastedBinaryOp(a, b, upcastType(a.dtype, b.dtype), function (aValue, bValue) { return aValue * bValue; });
- };
- MathBackendCPU.prototype.divide = function (a, b) {
- var op;
- var outputDtype;
- if (a.dtype === 'int32' && b.dtype === 'int32') {
- outputDtype = 'int32';
- op = function (a, b) { return Math.floor(a / b); };
- }
- else {
- outputDtype = 'float32';
- op = function (a, b) { return a / b; };
- }
- return this.broadcastedBinaryOp(a, b, outputDtype, op);
- };
- MathBackendCPU.prototype.sum = function (x, axes) {
- assertAxesAreInnerMostDims('sum', axes, x.rank);
- var _a = computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
- var resultDtype = upcastType(x.dtype, 'int32');
- var result = zeros(outShape, resultDtype);
- var reduceSize = sizeFromShape(reduceShape);
- var vals = result.dataSync();
- var aVals = x.dataSync();
- for (var i = 0; i < vals.length; ++i) {
- var offset = i * reduceSize;
- var sum$$1 = 0;
- for (var j = 0; j < reduceSize; ++j) {
- sum$$1 += aVals[offset + j];
- }
- vals[i] = sum$$1;
- }
- return result;
- };
- MathBackendCPU.prototype.argMin = function (x, axis) {
- var axes = [axis];
- assertAxesAreInnerMostDims('argMin', axes, x.rank);
- var _a = computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
- var result = zeros(outShape, 'int32');
- var reduceSize = sizeFromShape(reduceShape);
- var vals = result.dataSync();
- var aVals = x.dataSync();
- for (var i = 0; i < vals.length; ++i) {
- var offset = i * reduceSize;
- var min$$1 = aVals[offset];
- var minIndex = 0;
- for (var j = 0; j < reduceSize; ++j) {
- var value = aVals[offset + j];
- if (value < min$$1) {
- min$$1 = value;
- minIndex = j;
- }
- }
- vals[i] = minIndex;
- }
- return result;
- };
- MathBackendCPU.prototype.argMax = function (x, axis) {
- var axes = [axis];
- assertAxesAreInnerMostDims('argMax', axes, x.rank);
- var _a = computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
- var result = zeros(outShape, 'int32');
- var reduceSize = sizeFromShape(reduceShape);
- var vals = result.dataSync();
- var aVals = x.dataSync();
- for (var i = 0; i < vals.length; ++i) {
- var offset = i * reduceSize;
- var max$$1 = aVals[offset];
- var maxIndex = 0;
- for (var j = 0; j < reduceSize; ++j) {
- var value = aVals[offset + j];
- if (value > max$$1) {
- max$$1 = value;
- maxIndex = j;
- }
- }
- vals[i] = maxIndex;
- }
- return result;
- };
- MathBackendCPU.prototype.cumsum = function (x, axis, exclusive, reverse$$1) {
- var resultDtype = upcastType(x.dtype, 'int32');
- var result = zeros(x.shape, resultDtype);
- var vals = result.dataSync();
- var aVals = x.dataSync();
- var finalDim = x.shape[x.rank - 1];
- var indexAdjuster = reverse$$1 ?
- function (i, j) { return i + finalDim - j - 1; } :
- function (i, j) { return i + j; };
- for (var i = 0; i < aVals.length; i += finalDim) {
- for (var j = 0; j < finalDim; j++) {
- var idx = indexAdjuster(i, j);
- if (j === 0) {
- vals[idx] = exclusive ? 0 : aVals[idx];
- }
- else {
- var prevIdx = indexAdjuster(i, j - 1);
- vals[idx] = exclusive ? aVals[prevIdx] + vals[prevIdx] :
- aVals[idx] + vals[prevIdx];
- }
- }
- }
- return result;
- };
- MathBackendCPU.prototype.equal = function (a, b) {
- return this.broadcastedBinaryOp(a, b, 'bool', function (aVal, bVal) {
- return (aVal === bVal) ? 1 : 0;
- });
- };
- MathBackendCPU.prototype.notEqual = function (a, b) {
- return this.broadcastedBinaryOp(a, b, 'bool', function (aVal, bVal) {
- return (aVal !== bVal) ? 1 : 0;
- });
- };
- MathBackendCPU.prototype.less = function (a, b) {
- return this.broadcastedBinaryOp(a, b, 'bool', function (aVal, bVal) {
- return (aVal < bVal) ? 1 : 0;
- });
- };
- MathBackendCPU.prototype.lessEqual = function (a, b) {
- return this.broadcastedBinaryOp(a, b, 'bool', function (aVal, bVal) {
- return (aVal <= bVal) ? 1 : 0;
- });
- };
- MathBackendCPU.prototype.greater = function (a, b) {
- return this.broadcastedBinaryOp(a, b, 'bool', function (aVal, bVal) {
- return (aVal > bVal) ? 1 : 0;
- });
- };
- MathBackendCPU.prototype.greaterEqual = function (a, b) {
- return this.broadcastedBinaryOp(a, b, 'bool', function (aVal, bVal) {
- return (aVal >= bVal) ? 1 : 0;
- });
- };
- MathBackendCPU.prototype.logicalNot = function (x) {
- var values = x.dataSync();
- var newValues = new Int32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- newValues[i] = values[i] ? 0 : 1;
- }
- return Tensor.make(x.shape, { values: newValues }, 'bool');
- };
- MathBackendCPU.prototype.logicalAnd = function (a, b) {
- return this.broadcastedBinaryOp(a, b, 'bool', function (aVal, bVal) {
- return aVal && bVal;
- });
- };
- MathBackendCPU.prototype.logicalOr = function (a, b) {
- return this.broadcastedBinaryOp(a, b, 'bool', function (aVal, bVal) {
- return aVal || bVal;
- });
- };
- MathBackendCPU.prototype.where = function (condition, a, b, dtype) {
- var values = condition.dataSync();
- var aValues = a.dataSync();
- var bValues = b.dataSync();
- var result = zeros(a.shape, dtype);
- var newValues = result.dataSync();
- var index = 0;
- var offset = condition.rank === 0 || condition.rank > 1 || a.rank === 1 ?
- 1 :
- a.shape[1];
- for (var i = 0; i < values.length; i++) {
- for (var j = 0; j < offset; j++) {
- if (values[i] === 1) {
- newValues[index++] = aValues[i];
- }
- else {
- newValues[index++] = bValues[i];
- }
- }
- }
- return result;
- };
- MathBackendCPU.prototype.topKValues = function (x, k) {
- return this.topK(x, k).values;
- };
- MathBackendCPU.prototype.topKIndices = function (x, k) {
- return this.topK(x, k).indices;
- };
- MathBackendCPU.prototype.topK = function (x, k) {
- var values = x.dataSync();
- var valuesAndIndices = [];
- for (var i = 0; i < values.length; i++) {
- valuesAndIndices.push({ value: values[i], index: i });
- }
- valuesAndIndices.sort(function (a, b) {
- return b.value - a.value;
- });
- var topkValues = getTypedArrayFromDType(x.dtype, k);
- var topkIndices = new Int32Array(k);
- for (var i = 0; i < k; i++) {
- topkValues[i] = valuesAndIndices[i].value;
- topkIndices[i] = valuesAndIndices[i].index;
- }
- return {
- values: tensor1d(topkValues, x.dtype),
- indices: tensor1d(topkIndices, 'int32')
- };
- };
- MathBackendCPU.prototype.min = function (x, axes) {
- assertAxesAreInnerMostDims('min', axes, x.rank);
- var _a = computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
- var result = zeros(outShape, x.dtype);
- var reduceSize = sizeFromShape(reduceShape);
- var vals = result.dataSync();
- var aVals = x.dataSync();
- for (var i = 0; i < vals.length; ++i) {
- var offset = i * reduceSize;
- var min$$1 = aVals[0];
- for (var j = 0; j < reduceSize; ++j) {
- var value = aVals[offset + j];
- if (value < min$$1) {
- min$$1 = value;
- }
- }
- vals[i] = min$$1;
- }
- return result;
- };
- MathBackendCPU.prototype.minimum = function (a, b) {
- return this.broadcastedBinaryOp(a, b, a.dtype, function (aVal, bVal) { return Math.min(aVal, bVal); });
- };
- MathBackendCPU.prototype.mod = function (a, b) {
- return this.broadcastedBinaryOp(a, b, a.dtype, function (aVal, bVal) {
- var rem = aVal % bVal;
- if ((aVal < 0 && bVal < 0) || (aVal >= 0 && bVal >= 0)) {
- return rem;
- }
- else {
- return (rem + bVal) % bVal;
- }
- });
- };
- MathBackendCPU.prototype.max = function (x, axes) {
- assertAxesAreInnerMostDims('max', axes, x.rank);
- var _a = computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
- var result = zeros(outShape, x.dtype);
- var reduceSize = sizeFromShape(reduceShape);
- var vals = result.dataSync();
- var aVals = x.dataSync();
- for (var i = 0; i < vals.length; ++i) {
- var offset = i * reduceSize;
- var max$$1 = aVals[offset];
- for (var j = 0; j < reduceSize; ++j) {
- var value = aVals[offset + j];
- if (value > max$$1) {
- max$$1 = value;
- }
- }
- vals[i] = max$$1;
- }
- return result;
- };
- MathBackendCPU.prototype.maximum = function (a, b) {
- return this.broadcastedBinaryOp(a, b, a.dtype, function (aVal, bVal) { return Math.max(aVal, bVal); });
- };
- MathBackendCPU.prototype.squaredDifference = function (a, b) {
- return this.broadcastedBinaryOp(a, b, a.dtype, function (aVal, bVal) {
- var diff = aVal - bVal;
- return diff * diff;
- });
- };
- MathBackendCPU.prototype.ceil = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- newValues[i] = Math.ceil(values[i]);
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.floor = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- newValues[i] = Math.floor(values[i]);
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.sign = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- if (values[i] < 0) {
- newValues[i] = -1;
- }
- else if (values[i] > 0) {
- newValues[i] = 1;
- }
- else {
- newValues[i] = 0;
- }
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.round = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- var base = Math.floor(values[i]);
- if (values[i] - base < 0.5) {
- newValues[i] = Math.floor(values[i]);
- }
- else if (values[i] - base > 0.5) {
- newValues[i] = Math.ceil(values[i]);
- }
- else {
- if (base % 2.0 === 0.0) {
- newValues[i] = base;
- }
- else {
- newValues[i] = base + 1.0;
- }
- }
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.exp = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- newValues[i] = Math.exp(values[i]);
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.expm1 = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- newValues[i] = Math.expm1(values[i]);
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.log = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- var value = values[i];
- newValues[i] = Math.log(value);
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.log1p = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- var value = values[i];
- newValues[i] = Math.log1p(value);
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.sqrt = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- var value = values[i];
- newValues[i] = Math.sqrt(value);
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.rsqrt = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- var value = values[i];
- newValues[i] = 1 / Math.sqrt(value);
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.square = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- var value = values[i];
- newValues[i] = value * value;
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.reciprocal = function (x) {
- var values = x.dataSync();
- var newValues = new Float32Array(values.length);
- for (var i = 0; i < values.length; ++i) {
- newValues[i] = 1 / values[i];
- }
- return Tensor.make(x.shape, { values: newValues });
- };
- MathBackendCPU.prototype.relu = function (x) {
- var res = zeros(x.shape, x.dtype);
- var resVals = res.dataSync();
- var inVals = x.dataSync();
- for (var i = 0; i < inVals.length; ++i) {
- resVals[i] = Math.max(0, inVals[i]);
- }
- return res;
- };
- MathBackendCPU.prototype.elu = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- var v = values[i];
- if (v >= 0) {
- resultValues[i] = v;
- }
- else {
- resultValues[i] = (Math.exp(v) - 1);
- }
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.eluDer = function (dy, y) {
- var resultValues = new Float32Array(y.size);
- var values = y.dataSync();
- var dyValues = dy.dataSync();
- for (var i = 0; i < values.length; ++i) {
- var v = values[i];
- if (v >= 1) {
- resultValues[i] = dyValues[i];
- }
- else {
- resultValues[i] = dyValues[i] * (v + 1);
- }
- }
- return Tensor.make(y.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.selu = function (x) {
- var scaleAlpha = SELU_SCALEALPHA;
- var scale = SELU_SCALE;
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- var v = values[i];
- if (v >= 0) {
- resultValues[i] = scale * v;
- }
- else {
- resultValues[i] = scaleAlpha * (Math.exp(v) - 1);
- }
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.clip = function (x, min$$1, max$$1) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.min(max$$1, Math.max(min$$1, values[i]));
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.abs = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.abs(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.int = function (x) {
- var resultValues = new Int32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = values[i];
- }
- return Tensor.make(x.shape, { values: resultValues }, 'int32');
- };
- MathBackendCPU.prototype.sigmoid = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = 1 / (1 + Math.exp(-values[i]));
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.softplus = function (x) {
- var epsilon = 1.1920928955078125e-7;
- var threshold = Math.log(epsilon) + 2.0;
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- var tooLarge = values[i] > -threshold;
- var tooSmall = values[i] < threshold;
- var expX = Math.exp(values[i]);
- var result = void 0;
- if (tooSmall) {
- result = expX;
- }
- else if (tooLarge) {
- result = values[i];
- }
- else {
- result = Math.log(1.0 + expX);
- }
- resultValues[i] = result;
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.sin = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.sin(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.cos = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.cos(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.tan = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.tan(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.asin = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.asin(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.acos = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.acos(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.atan = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.atan(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.atan2 = function (a, b) {
- return this.broadcastedBinaryOp(a, b, a.dtype, function (aValue, bValue) { return Math.atan2(aValue, bValue); });
- };
- MathBackendCPU.prototype.sinh = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.sinh(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.cosh = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.cosh(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.tanh = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = tanh(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.asinh = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.asinh(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.acosh = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.acosh(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.atanh = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- resultValues[i] = Math.atanh(values[i]);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.erf = function (x) {
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- var p = ERF_P;
- var a1 = ERF_A1;
- var a2 = ERF_A2;
- var a3 = ERF_A3;
- var a4 = ERF_A4;
- var a5 = ERF_A5;
- for (var i = 0; i < values.length; ++i) {
- var v = values[i];
- var t = 1.0 / (1.0 + p * v);
- resultValues[i] = 1.0 -
- (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t *
- Math.exp(-v * v);
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.step = function (x, alpha) {
- if (alpha === void 0) { alpha = 0; }
- var resultValues = new Float32Array(x.size);
- var values = x.dataSync();
- for (var i = 0; i < values.length; ++i) {
- var value = values[i];
- if (isNaN(value)) {
- resultValues[i] = NaN;
- }
- else {
- resultValues[i] = value > 0 ? 1 : alpha;
- }
- }
- return Tensor.make(x.shape, { values: resultValues });
- };
- MathBackendCPU.prototype.conv2d = function (x, filter, convInfo) {
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var dilationHeight = convInfo.dilationHeight;
- var dilationWidth = convInfo.dilationWidth;
- var padLeft = convInfo.padInfo.left;
- var padTop = convInfo.padInfo.top;
- var y = buffer(convInfo.outShape, x.dtype);
- for (var b = 0; b < convInfo.batchSize; ++b) {
- for (var d2 = 0; d2 < convInfo.outChannels; ++d2) {
- for (var yR = 0; yR < convInfo.outHeight; ++yR) {
- var xRCorner = yR * convInfo.strideHeight - padLeft;
- for (var yC = 0; yC < convInfo.outWidth; ++yC) {
- var xCCorner = yC * convInfo.strideWidth - padTop;
- var dotProd = 0;
- for (var wR = 0; wR < filterHeight; wR++) {
- var xR = xRCorner + wR * dilationHeight;
- if (xR < 0 || xR >= convInfo.inHeight) {
- continue;
- }
- for (var wC = 0; wC < filterWidth; wC++) {
- var xC = xCCorner + wC * dilationWidth;
- if (xC < 0 || xC >= convInfo.inWidth) {
- continue;
- }
- for (var d1 = 0; d1 < convInfo.inChannels; ++d1) {
- var pixel = x.get(b, xR, xC, d1);
- var weight = filter.get(wR, wC, d1, d2);
- dotProd += pixel * weight;
- }
- }
- }
- y.set(dotProd, b, yR, yC, d2);
- }
- }
- }
- }
- return y.toTensor();
- };
- MathBackendCPU.prototype.conv2dDerInput = function (dy, filter, convInfo) {
- var dx = buffer(convInfo.inShape, 'float32');
- var dxValues = dx.values;
- var _a = dx.strides, dxS0 = _a[0], dxS1 = _a[1], dxS2 = _a[2];
- var dyValues = dy.dataSync();
- var _b = dy.strides, dyS0 = _b[0], dyS1 = _b[1], dyS2 = _b[2];
- var fltValues = filter.dataSync();
- var _c = filter.strides, fltS0 = _c[0], fltS1 = _c[1], fltS2 = _c[2];
- var batchSize = convInfo.batchSize, filterHeight = convInfo.filterHeight, filterWidth = convInfo.filterWidth, inChannels = convInfo.inChannels, inHeight = convInfo.inHeight, inWidth = convInfo.inWidth, outChannels = convInfo.outChannels, outHeight = convInfo.outHeight, outWidth = convInfo.outWidth, strideHeight = convInfo.strideHeight, strideWidth = convInfo.strideWidth;
- var topPad = filterHeight - 1 - convInfo.padInfo.top;
- var leftPad = filterWidth - 1 - convInfo.padInfo.left;
- for (var b = 0; b < batchSize; ++b) {
- for (var d1 = 0; d1 < inChannels; ++d1) {
- for (var xR = 0; xR < inHeight; ++xR) {
- var xRCorner = xR - topPad;
- var xRMin = Math.max(0, Math.ceil(xRCorner / strideHeight));
- var yRMax = Math.min(outHeight, (filterHeight + xRCorner) / strideHeight);
- for (var xC = 0; xC < inWidth; ++xC) {
- var xCCorner = xC - leftPad;
- var xCMin = Math.max(0, Math.ceil(xCCorner / strideWidth));
- var yCMax = Math.min(outWidth, (filterWidth + xCCorner) / strideWidth);
- var dotProd = 0;
- for (var yR = xRMin; yR < yRMax; ++yR) {
- var wR = yR * strideHeight - xRCorner;
- for (var yC = xCMin; yC < yCMax; ++yC) {
- var wC = yC * strideWidth - xCCorner;
- var dyOffset = dyS0 * b + dyS1 * yR + dyS2 * yC;
- var fltOffset = fltS0 * (filterHeight - 1 - wR) +
- fltS1 * (filterWidth - 1 - wC) + fltS2 * d1;
- for (var d2 = 0; d2 < outChannels; ++d2) {
- var pixel = dyValues[dyOffset + d2];
- var weight = fltValues[fltOffset + d2];
- dotProd += pixel * weight;
- }
- }
- }
- dxValues[dxS0 * b + dxS1 * xR + dxS2 * xC + d1] = dotProd;
- }
- }
- }
- }
- return dx.toTensor();
- };
- MathBackendCPU.prototype.conv2dDerFilter = function (x, dy, convInfo) {
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var dW = buffer(convInfo.filterShape, 'float32');
- var leftPad = convInfo.padInfo.left;
- var topPad = convInfo.padInfo.top;
- for (var wR = 0; wR < filterHeight; ++wR) {
- var yRMin = Math.max(0, Math.ceil((topPad - wR) / strideHeight));
- var yRMax = Math.min(convInfo.outHeight, (convInfo.inHeight + topPad - wR) / strideHeight);
- for (var wC = 0; wC < filterWidth; ++wC) {
- var yCMin = Math.max(0, Math.ceil((leftPad - wC) / strideWidth));
- var yCMax = Math.min(convInfo.outWidth, (convInfo.inWidth + leftPad - wC) / strideWidth);
- for (var d1 = 0; d1 < convInfo.inChannels; ++d1) {
- for (var d2 = 0; d2 < convInfo.outChannels; ++d2) {
- var dotProd = 0;
- for (var b = 0; b < convInfo.batchSize; ++b) {
- for (var yR = yRMin; yR < yRMax; ++yR) {
- var xR = wR + yR * strideHeight - topPad;
- for (var yC = yCMin; yC < yCMax; ++yC) {
- var xC = wC + yC * strideWidth - leftPad;
- dotProd += x.get(b, xR, xC, d1) * dy.get(b, yR, yC, d2);
- }
- }
- }
- dW.set(dotProd, wR, wC, d1, d2);
- }
- }
- }
- }
- return dW.toTensor();
- };
- MathBackendCPU.prototype.depthwiseConv2D = function (x, filter, convInfo) {
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var dilationHeight = convInfo.dilationHeight;
- var dilationWidth = convInfo.dilationWidth;
- var padLeft = convInfo.padInfo.left;
- var padTop = convInfo.padInfo.top;
- var chMul = convInfo.outChannels / convInfo.inChannels;
- var y = buffer(convInfo.outShape, x.dtype);
- for (var b = 0; b < convInfo.batchSize; ++b) {
- for (var d1 = 0; d1 < convInfo.inChannels; ++d1) {
- for (var yR = 0; yR < convInfo.outHeight; ++yR) {
- var xRCorner = yR * convInfo.strideHeight - padLeft;
- for (var yC = 0; yC < convInfo.outWidth; ++yC) {
- var xCCorner = yC * convInfo.strideWidth - padTop;
- for (var q = 0; q < chMul; ++q) {
- var dotProd = 0;
- for (var wR = 0; wR < filterHeight; ++wR) {
- var xR = xRCorner + wR * dilationHeight;
- if (xR < 0 || xR >= convInfo.inHeight) {
- continue;
- }
- for (var wC = 0; wC < filterWidth; ++wC) {
- var xC = xCCorner + wC * dilationWidth;
- if (xC < 0 || xC >= convInfo.inWidth) {
- continue;
- }
- var pixel = x.get(b, xR, xC, d1);
- var weight = filter.get(wR, wC, d1, q);
- dotProd += pixel * weight;
- }
- }
- y.set(dotProd, b, yR, yC, d1 * chMul + q);
- }
- }
- }
- }
- }
- return y.toTensor();
- };
- MathBackendCPU.prototype.tile = function (x, reps) {
- var newShape = new Array(x.rank);
- for (var i = 0; i < newShape.length; i++) {
- newShape[i] = x.shape[i] * reps[i];
- }
- var result = buffer(newShape, x.dtype);
- var xBuf = x.buffer();
- for (var i = 0; i < result.values.length; ++i) {
- var newLoc = result.indexToLoc(i);
- var originalLoc = new Array(x.rank);
- for (var i_1 = 0; i_1 < originalLoc.length; i_1++) {
- originalLoc[i_1] = newLoc[i_1] % x.shape[i_1];
- }
- var originalIndex = xBuf.locToIndex(originalLoc);
- result.values[i] = xBuf.values[originalIndex];
- }
- return result.toTensor();
- };
- MathBackendCPU.prototype.pad = function (x, paddings, constantValue) {
- var outShape = paddings.map(function (p, i) { return p[0] + x.shape[i] + p[1]; });
- var start = paddings.map(function (p) { return p[0]; });
- var xBuffer = x.buffer();
- var buffer$$1 = buffer(outShape, x.dtype);
- if (constantValue !== 0) {
- buffer$$1.values.fill(constantValue);
- }
- for (var i = 0; i < x.size; i++) {
- var coords = xBuffer.indexToLoc(i);
- var outCoords = coords.map(function (c, i) { return c + start[i]; });
- buffer$$1.set.apply(buffer$$1, [x.get.apply(x, coords)].concat(outCoords));
- }
- return buffer$$1.toTensor();
- };
- MathBackendCPU.prototype.transpose = function (x, perm) {
- var newShape = new Array(x.rank);
- for (var i = 0; i < newShape.length; i++) {
- newShape[i] = x.shape[perm[i]];
- }
- var values = x.dataSync();
- var result = buffer(newShape, x.dtype);
- var xBuf = x.buffer();
- for (var i = 0; i < x.size; ++i) {
- var loc = xBuf.indexToLoc(i);
- var newLoc = new Array(loc.length);
- for (var i_2 = 0; i_2 < newLoc.length; i_2++) {
- newLoc[i_2] = loc[perm[i_2]];
- }
- var newIndex = result.locToIndex(newLoc);
- result.values[newIndex] = values[i];
- }
- return result.toTensor();
- };
- MathBackendCPU.prototype.gather = function (x, indices, axis) {
- var newShape = x.shape.slice();
- var indicesValues = indices.dataSync();
- newShape[axis] = indicesValues.length;
- var result = buffer(newShape, x.dtype);
- var xBuf = x.buffer();
- for (var i = 0; i < result.size; ++i) {
- var newLoc = result.indexToLoc(i);
- var originalLoc = newLoc.slice();
- originalLoc[axis] = indicesValues[newLoc[axis]];
- var originalIndex = xBuf.locToIndex(originalLoc);
- result.values[i] = xBuf.values[originalIndex];
- }
- return result.toTensor();
- };
- MathBackendCPU.prototype.pool = function (x, convInfo, poolType) {
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var y = buffer(convInfo.outShape, 'float32');
- var padTop = convInfo.padInfo.top;
- var padLeft = convInfo.padInfo.left;
- for (var b = 0; b < convInfo.batchSize; ++b) {
- for (var d = 0; d < convInfo.inChannels; ++d) {
- for (var yR = 0; yR < convInfo.outHeight; ++yR) {
- var xRCorner = yR * strideHeight - padTop;
- var xRMin = Math.max(0, xRCorner);
- var xRMax = Math.min(convInfo.inHeight, filterHeight + xRCorner);
- for (var yC = 0; yC < convInfo.outWidth; ++yC) {
- var xCCorner = yC * strideWidth - padLeft;
- var xCMin = Math.max(0, xCCorner);
- var xCMax = Math.min(convInfo.inWidth, filterWidth + xCCorner);
- var minMaxValue = (poolType === 'max' ? Number.NEGATIVE_INFINITY :
- Number.POSITIVE_INFINITY);
- var avgValue = 0;
- var count = 0;
- for (var xR = xRMin; xR < xRMax; ++xR) {
- for (var xC = xCMin; xC < xCMax; ++xC) {
- var pixel = x.get(b, xR, xC, d);
- if ((poolType === 'max' && pixel > minMaxValue)) {
- minMaxValue = pixel;
- }
- else if (poolType === 'avg') {
- avgValue += pixel;
- count++;
- }
- }
- if (isNaN(minMaxValue)) {
- break;
- }
- }
- y.set(poolType === 'avg' ? avgValue / count : minMaxValue, b, yR, yC, d);
- }
- }
- }
- }
- return y.toTensor();
- };
- MathBackendCPU.prototype.maxPool = function (x, convInfo) {
- return this.pool(x, convInfo, 'max');
- };
- MathBackendCPU.prototype.maxPoolPositions = function (x, convInfo) {
- var maxPositions = buffer(convInfo.outShape, 'int32');
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var padTop = convInfo.padInfo.top;
- var padLeft = convInfo.padInfo.left;
- for (var b = 0; b < convInfo.batchSize; ++b) {
- for (var d = 0; d < convInfo.inChannels; ++d) {
- for (var yR = 0; yR < convInfo.outHeight; ++yR) {
- var xRCorner = yR * strideHeight - padTop;
- var xRMin = Math.max(0, xRCorner);
- var xRMax = Math.min(convInfo.inHeight, filterHeight + xRCorner);
- for (var yC = 0; yC < convInfo.outWidth; ++yC) {
- var xCCorner = yC * strideWidth - padLeft;
- var xCMin = Math.max(0, xCCorner);
- var xCMax = Math.min(convInfo.inWidth, filterWidth + xCCorner);
- var maxValue = Number.NEGATIVE_INFINITY;
- var maxPosition = -1;
- for (var xR = xRMin; xR < xRMax; ++xR) {
- var wR = xR - xRCorner;
- for (var xC = xCMin; xC < xCMax; ++xC) {
- var wC = xC - xCCorner;
- var pixel = x.get(b, xR, xC, d);
- if (pixel > maxValue) {
- maxValue = pixel;
- maxPosition = wR * filterWidth + wC;
- }
- }
- }
- maxPositions.set(maxPosition, b, yR, yC, d);
- }
- }
- }
- }
- return maxPositions.toTensor();
- };
- MathBackendCPU.prototype.maxPoolBackprop = function (dy, x, y, convInfo) {
- var maxPositions = this.maxPoolPositions(x, convInfo);
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var padLeft = filterWidth - 1 - convInfo.padInfo.left;
- var padTop = filterHeight - 1 - convInfo.padInfo.top;
- var dx = buffer(x.shape, 'float32');
- for (var b = 0; b < convInfo.batchSize; ++b) {
- for (var d = 0; d < convInfo.inChannels; ++d) {
- for (var dxR = 0; dxR < convInfo.inHeight; ++dxR) {
- for (var dxC = 0; dxC < convInfo.inWidth; ++dxC) {
- var dyRCorner = dxR - padTop;
- var dyCCorner = dxC - padLeft;
- var dotProd = 0;
- for (var wR = 0; wR < filterHeight; ++wR) {
- var dyR = (dyRCorner + wR) / strideHeight;
- if (dyR < 0 || dyR >= convInfo.outHeight ||
- Math.floor(dyR) !== dyR) {
- continue;
- }
- for (var wC = 0; wC < filterWidth; ++wC) {
- var dyC = (dyCCorner + wC) / strideWidth;
- if (dyC < 0 || dyC >= convInfo.outWidth ||
- Math.floor(dyC) !== dyC) {
- continue;
- }
- var maxPos = filterHeight * filterWidth - 1 -
- maxPositions.get(b, dyR, dyC, d);
- var curPos = wR * filterWidth + wC;
- var mask = maxPos === curPos ? 1 : 0;
- if (mask === 0) {
- continue;
- }
- var pixel = dy.get(b, dyR, dyC, d);
- dotProd += pixel * mask;
- }
- }
- dx.set(dotProd, b, dxR, dxC, d);
- }
- }
- }
- }
- return dx.toTensor();
- };
- MathBackendCPU.prototype.avgPoolBackprop = function (dy, x, convInfo) {
- var strideHeight = convInfo.strideHeight;
- var strideWidth = convInfo.strideWidth;
- var filterHeight = convInfo.filterHeight;
- var filterWidth = convInfo.filterWidth;
- var padLeft = filterWidth - 1 - convInfo.padInfo.left;
- var padTop = filterHeight - 1 - convInfo.padInfo.top;
- var dx = buffer(x.shape, 'float32');
- var avgMultiplier = 1 / (filterHeight * filterWidth);
- for (var b = 0; b < convInfo.batchSize; ++b) {
- for (var d = 0; d < convInfo.inChannels; ++d) {
- for (var dxR = 0; dxR < convInfo.inHeight; ++dxR) {
- for (var dxC = 0; dxC < convInfo.inWidth; ++dxC) {
- var dyRCorner = dxR - padTop;
- var dyCCorner = dxC - padLeft;
- var dotProd = 0;
- for (var wR = 0; wR < filterHeight; ++wR) {
- var dyR = (dyRCorner + wR) / strideHeight;
- if (dyR < 0 || dyR >= convInfo.outHeight ||
- Math.floor(dyR) !== dyR) {
- continue;
- }
- for (var wC = 0; wC < filterWidth; ++wC) {
- var dyC = (dyCCorner + wC) / strideWidth;
- if (dyC < 0 || dyC >= convInfo.outWidth ||
- Math.floor(dyC) !== dyC) {
- continue;
- }
- var pixel = dy.get(b, dyR, dyC, d);
- dotProd += pixel;
- }
- }
- dx.set(dotProd * avgMultiplier, b, dxR, dxC, d);
- }
- }
- }
- }
- return dx.toTensor();
- };
- MathBackendCPU.prototype.cast = function (x, dtype) {
- return castTensor(x, dtype, this);
- };
- MathBackendCPU.prototype.reshape = function (x, shape) {
- return reshapeTensor(x, shape);
- };
- MathBackendCPU.prototype.avgPool = function (x, convInfo) {
- return this.pool(x, convInfo, 'avg').toFloat();
- };
- MathBackendCPU.prototype.resizeBilinear = function (x, newHeight, newWidth, alignCorners) {
- var _a = x.shape, batch = _a[0], oldHeight = _a[1], oldWidth = _a[2], numChannels = _a[3];
- var output = buffer([batch, newHeight, newWidth, numChannels], x.dtype);
- var effectiveInputSize = [
- (alignCorners && newHeight > 1) ? oldHeight - 1 : oldHeight,
- (alignCorners && newWidth > 1) ? oldWidth - 1 : oldWidth
- ];
- var effectiveOutputSize = [
- (alignCorners && newHeight > 1) ? newHeight - 1 : newHeight,
- (alignCorners && newWidth > 1) ? newWidth - 1 : newWidth
- ];
- for (var b = 0; b < batch; b++) {
- for (var r = 0; r < newHeight; r++) {
- for (var c = 0; c < newWidth; c++) {
- for (var d = 0; d < numChannels; d++) {
- var sourceFracRow = (effectiveInputSize[0]) * r / (effectiveOutputSize[0]);
- var sourceFracCol = (effectiveInputSize[1]) * c / (effectiveOutputSize[1]);
- var sourceRowFloor = Math.floor(sourceFracRow);
- var sourceRowCeil = Math.min(oldHeight - 1, Math.ceil(sourceFracRow));
- var sourceColFloor = Math.floor(sourceFracCol);
- var sourceColCeil = Math.min(oldWidth - 1, Math.ceil(sourceFracCol));
- var topLeft = x.get(b, sourceRowFloor, sourceColFloor, d);
- var bottomLeft = x.get(b, sourceRowCeil, sourceColFloor, d);
- var topRight = x.get(b, sourceRowFloor, sourceColCeil, d);
- var bottomRight = x.get(b, sourceRowCeil, sourceColCeil, d);
- var rowFrac = sourceFracRow - sourceRowFloor;
- var colFrac = sourceFracCol - sourceColFloor;
- var top_1 = topLeft + (topRight - topLeft) * colFrac;
- var bottom = bottomLeft + (bottomRight - bottomLeft) * colFrac;
- var newValue = top_1 + (bottom - top_1) * rowFrac;
- output.set(newValue, b, r, c, d);
- }
- }
- }
- }
- return output.toTensor();
- };
- MathBackendCPU.prototype.resizeBilinearBackprop = function (dy, x, alignCorners) {
- var _a = x.shape, batch = _a[0], xHeight = _a[1], xWidth = _a[2], depth = _a[3];
- var _b = dy.shape, yHeight = _b[1], yWidth = _b[2];
- var output = buffer([batch, xHeight, xWidth, depth], x.dtype);
- var effectiveXSize = [
- (alignCorners && yHeight > 1) ? xHeight - 1 : xHeight,
- (alignCorners && yWidth > 1) ? xWidth - 1 : xWidth
- ];
- var effectiveYSize = [
- (alignCorners && yHeight > 1) ? yHeight - 1 : yHeight,
- (alignCorners && yWidth > 1) ? yWidth - 1 : yWidth
- ];
- var heightScale = effectiveXSize[0] / effectiveYSize[0];
- var widthScale = effectiveXSize[1] / effectiveYSize[1];
- for (var b = 0; b < batch; b++) {
- for (var r = 0; r < yHeight; r++) {
- var dxR = r * heightScale;
- var topDxRIndex = Math.floor(dxR);
- var bottomDxRIndex = Math.min(Math.ceil(dxR), xHeight - 1);
- var dxRLerp = dxR - topDxRIndex;
- var inverseDxRLerp = 1.0 - dxRLerp;
- for (var c = 0; c < yWidth; c++) {
- var dxC = c * widthScale;
- var leftDxCIndex = Math.floor(dxC);
- var rightDxCIndex = Math.min(Math.ceil(dxC), xWidth - 1);
- var dxCLerp = dxC - leftDxCIndex;
- var inverseDxCLerp = 1.0 - dxCLerp;
- for (var d = 0; d < depth; d++) {
- var dyVal = dy.get(b, r, c, d);
- var topLeft = output.get(b, topDxRIndex, leftDxCIndex, d);
- topLeft += dyVal * inverseDxRLerp * inverseDxCLerp;
- output.set(topLeft, b, topDxRIndex, leftDxCIndex, d);
- var topRight = output.get(b, topDxRIndex, rightDxCIndex, d);
- topRight += dyVal * inverseDxRLerp * dxCLerp;
- output.set(topRight, b, topDxRIndex, rightDxCIndex, d);
- var bottomLeft = output.get(b, bottomDxRIndex, leftDxCIndex, d);
- bottomLeft += dyVal * dxRLerp * inverseDxCLerp;
- output.set(bottomLeft, b, bottomDxRIndex, leftDxCIndex, d);
- var bottomRight = output.get(b, bottomDxRIndex, rightDxCIndex, d);
- bottomRight += dyVal * dxRLerp * dxCLerp;
- output.set(bottomRight, b, bottomDxRIndex, rightDxCIndex, d);
- }
- }
- }
- }
- return output.toTensor();
- };
- MathBackendCPU.prototype.resizeNearestNeighbor = function (x, newHeight, newWidth, alignCorners) {
- var _a = x.shape, batch = _a[0], oldHeight = _a[1], oldWidth = _a[2], numChannels = _a[3];
- var output = buffer([batch, newHeight, newWidth, numChannels], x.dtype);
- var effectiveInputSize = alignCorners ? [oldHeight - 1, oldWidth - 1] : [oldHeight, oldWidth];
- var effectiveOutputSize = alignCorners ? [newHeight - 1, newWidth - 1] : [newHeight, newWidth];
- for (var b = 0; b < batch; b++) {
- for (var r = 0; r < newHeight; r++) {
- for (var c = 0; c < newWidth; c++) {
- for (var d = 0; d < numChannels; d++) {
- var sourceFracRow = (effectiveInputSize[0]) * r / (effectiveOutputSize[0]);
- var sourceFracCol = (effectiveInputSize[1]) * c / (effectiveOutputSize[1]);
- var sourceNearestRow = Math.min(oldHeight - 1, alignCorners ? Math.round(sourceFracRow) :
- Math.floor(sourceFracRow));
- var sourceNearestCol = Math.min(oldWidth - 1, alignCorners ? Math.round(sourceFracCol) :
- Math.floor(sourceFracCol));
- var newValue = x.get(b, sourceNearestRow, sourceNearestCol, d);
- output.set(newValue, b, r, c, d);
- }
- }
- }
- }
- return output.toTensor();
- };
- MathBackendCPU.prototype.batchNormalization = function (x, mean$$1, variance, varianceEpsilon, scale, offset) {
- var xValues = x.dataSync();
- var meanValues = mean$$1.dataSync();
- var varianceValues = variance.dataSync();
- var scaleValues = scale ? scale.dataSync() : new Float32Array([1]);
- var offsetValues = offset ? offset.dataSync() : new Float32Array([0]);
- var outValues = new Float32Array(xValues.length);
- for (var i = 0; i < xValues.length; i++) {
- outValues[i] = offsetValues[i % offsetValues.length] +
- (xValues[i] - meanValues[i % meanValues.length]) *
- scaleValues[i % scaleValues.length] /
- Math.sqrt(varianceValues[i % varianceValues.length] + varianceEpsilon);
- }
- return tensor4d(outValues, x.shape);
- };
- MathBackendCPU.prototype.localResponseNormalization4D = function (x, radius, bias, alpha, beta) {
- var output = buffer(x.shape, 'float32');
- var rad = radius;
- var maxD = output.shape[3] - 1;
- function sumAcrossChannels(b, r, c, d) {
- var sum$$1 = 0.0;
- for (var j = Math.max(0, d - rad); j <= Math.min(d + rad, maxD); j++) {
- var z = x.get(b, r, c, j);
- sum$$1 += z * z;
- }
- return sum$$1;
- }
- for (var b = 0; b < output.shape[0]; b++) {
- for (var r = 0; r <= output.shape[1]; r++) {
- for (var c = 0; c < output.shape[2]; c++) {
- for (var d = 0; d < output.shape[3]; d++) {
- var sum$$1 = sumAcrossChannels(b, r, c, d);
- var val = x.get(b, r, c, d) * Math.pow(bias + alpha * sum$$1, -beta);
- output.set(val, b, r, c, d);
- }
- }
- }
- }
- return output.toTensor();
- };
- MathBackendCPU.prototype.multinomial = function (logits, normalized, numSamples, seed) {
- var probabilities = normalized ? logits : softmax(logits);
- var batchSize = probabilities.shape[0];
- var numEvents = probabilities.shape[1];
- var res = zeros([batchSize, numSamples], 'int32');
- var resVals = res.dataSync();
- var probVals = probabilities.dataSync();
- for (var b = 0; b < batchSize; ++b) {
- var offset = b * numEvents;
- var cdf = new Float32Array(numEvents - 1);
- cdf[0] = probVals[offset];
- for (var event_1 = 1; event_1 < cdf.length; ++event_1) {
- cdf[event_1] = cdf[event_1 - 1] + probVals[offset + event_1];
- }
- var random = undefined(seed.toString());
- var outOffset = b * numSamples;
- for (var sampleId = 0; sampleId < numSamples; ++sampleId) {
- var r = random();
- resVals[outOffset + sampleId] = cdf.length;
- for (var event_2 = 0; event_2 < cdf.length; event_2++) {
- if (r < cdf[event_2]) {
- resVals[outOffset + sampleId] = event_2;
- break;
- }
- }
- }
- }
- return res;
- };
- MathBackendCPU.prototype.oneHot = function (indices, depth, onValue, offValue) {
- var res = new Float32Array(indices.size * depth);
- res.fill(offValue);
- for (var event_3 = 0; event_3 < indices.size; ++event_3) {
- res[event_3 * depth + indices.get(event_3)] = onValue;
- }
- return tensor2d(res, [indices.size, depth]);
- };
- MathBackendCPU.prototype.broadcastedBinaryOp = function (a, b, dtype, op) {
- var newShape = assertAndGetBroadcastShape(a.shape, b.shape);
- var result = buffer(newShape, dtype);
- var aValues = a.dataSync();
- var bValues = b.dataSync();
- var aBroadcastDims = getBroadcastDims(a.shape, newShape);
- var bBroadcastDims = getBroadcastDims(b.shape, newShape);
- var aBuf = a.buffer();
- var bBuf = b.buffer();
- var _loop_2 = function (i) {
- var loc = result.indexToLoc(i);
- var aLoc = loc.slice(-a.rank);
- aBroadcastDims.forEach(function (d) { return aLoc[d] = 0; });
- var aIndex = aBuf.locToIndex(aLoc);
- var bLoc = loc.slice(-b.rank);
- bBroadcastDims.forEach(function (d) { return bLoc[d] = 0; });
- var bIndex = bBuf.locToIndex(bLoc);
- result.values[i] = op(aValues[aIndex], bValues[bIndex]);
- };
- for (var i = 0; i < result.values.length; ++i) {
- _loop_2(i);
- }
- return result.toTensor();
- };
- MathBackendCPU.prototype.dispose = function () { };
- return MathBackendCPU;
- }());
- ENV.registerBackend('cpu', function () { return new MathBackendCPU(); }, 1);
-
- var __decorate$r = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var BrowserUtil = (function () {
- function BrowserUtil() {
- }
- BrowserUtil.nextFrame = function () {
- return new Promise(function (resolve) { return requestAnimationFrame(function () { return resolve(); }); });
- };
- __decorate$r([
- doc({ heading: 'Performance', subheading: 'Timing' })
- ], BrowserUtil, "nextFrame", null);
- return BrowserUtil;
- }());
-
- var DTYPE_VALUE_SIZE_MAP = {
- 'float32': 4,
- 'int32': 4,
- 'uint16': 2,
- 'uint8': 1,
- 'bool': 1,
- };
-
- var __awaiter$7 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$7 = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- function encodeWeights(tensors) {
- return __awaiter$7(this, void 0, void 0, function () {
- var specs, dataPromises, name_1, t, tensorValues;
- return __generator$7(this, function (_a) {
- switch (_a.label) {
- case 0:
- specs = [];
- dataPromises = [];
- for (name_1 in tensors) {
- t = tensors[name_1];
- if (t.dtype !== 'float32' && t.dtype !== 'int32' && t.dtype !== 'bool') {
- throw new Error("Unsupported dtype in weight '" + name_1 + "': " + t.dtype);
- }
- specs.push({ name: name_1, shape: t.shape, dtype: t.dtype });
- dataPromises.push(t.data());
- }
- return [4, Promise.all(dataPromises)];
- case 1:
- tensorValues = _a.sent();
- return [2, { data: concatenateTypedArrays(tensorValues), specs: specs }];
- }
- });
- });
- }
- function decodeWeights(buffer, specs) {
- var out = {};
- var offset = 0;
- for (var _i = 0, specs_1 = specs; _i < specs_1.length; _i++) {
- var spec = specs_1[_i];
- var name_2 = spec.name;
- var dtype = spec.dtype;
- var shape = spec.shape;
- if (spec.quantization != null) {
- throw new Error("decodeWeights does not support quantization yet, but encountered " +
- ("weight '" + name_2 + " with quantization.'"));
- }
- var size = sizeFromShape(shape);
- var value = void 0;
- if (dtype === 'float32') {
- value = ArrayOps.tensor(new Float32Array(buffer, offset, size), shape, 'float32');
- }
- else if (dtype === 'int32') {
- value =
- ArrayOps.tensor(new Int32Array(buffer, offset, size), shape, 'int32');
- }
- else if (dtype === 'bool') {
- value =
- ArrayOps.tensor(new Uint8Array(buffer, offset, size), shape, 'bool');
- }
- else {
- throw new Error("Unsupported dtype in weight '" + name_2 + "': " + dtype);
- }
- out[name_2] = value;
- offset += size * DTYPE_VALUE_SIZE_MAP[dtype];
- }
- return out;
- }
- function concatenateTypedArrays(xs) {
- if (xs === null) {
- throw new Error("Invalid input value: " + JSON.stringify(xs));
- }
- var totalByteLength = 0;
- xs.forEach(function (x) {
- if (x instanceof Float32Array || x instanceof Int32Array) {
- totalByteLength += x.length * 4;
- }
- else if (x instanceof Uint8Array) {
- totalByteLength += x.length;
- }
- else {
- throw new Error("Unsupported TypedArray subtype: " + x.constructor.name);
- }
- });
- var y = new Uint8Array(totalByteLength);
- var offset = 0;
- xs.forEach(function (x) {
- y.set(new Uint8Array(x.buffer), offset);
- if (x instanceof Float32Array || x instanceof Int32Array) {
- offset += x.length * 4;
- }
- else {
- offset += x.length;
- }
- });
- return y.buffer;
- }
- function stringByteLength(str) {
- return new Blob([str]).size;
- }
- function arrayBufferToBase64String(buffer) {
- return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
- }
- function base64StringToArrayBuffer(str) {
- var s = atob(str);
- var buffer = new Uint8Array(s.length);
- for (var i = 0; i < s.length; ++i) {
- buffer.set([s.charCodeAt(i)], i);
- }
- return buffer.buffer;
- }
- function concatenateArrayBuffers(buffers) {
- var totalByteLength = 0;
- buffers.forEach(function (buffer) {
- totalByteLength += buffer.byteLength;
- });
- var temp = new Uint8Array(totalByteLength);
- var offset = 0;
- buffers.forEach(function (buffer) {
- temp.set(new Uint8Array(buffer), offset);
- offset += buffer.byteLength;
- });
- return temp.buffer;
- }
- function basename(path) {
- var SEPARATOR = '/';
- path = path.trim();
- while (path.endsWith(SEPARATOR)) {
- path = path.slice(0, path.length - 1);
- }
- var items = path.split(SEPARATOR);
- return items[items.length - 1];
- }
- function getModelArtifactsInfoForJSON(modelArtifacts) {
- if (modelArtifacts.modelTopology instanceof ArrayBuffer) {
- throw new Error('Expected JSON model topology, received ArrayBuffer.');
- }
- return {
- dateSaved: new Date(),
- modelTopologyType: 'JSON',
- modelTopologyBytes: modelArtifacts.modelTopology == null ?
- 0 :
- stringByteLength(JSON.stringify(modelArtifacts.modelTopology)),
- weightSpecsBytes: modelArtifacts.weightSpecs == null ?
- 0 :
- stringByteLength(JSON.stringify(modelArtifacts.weightSpecs)),
- weightDataBytes: modelArtifacts.weightData == null ?
- 0 :
- modelArtifacts.weightData.byteLength,
- };
- }
-
- var IORouterRegistry = (function () {
- function IORouterRegistry() {
- this.saveRouters = [];
- this.loadRouters = [];
- }
- IORouterRegistry.getInstance = function () {
- if (IORouterRegistry.instance == null) {
- IORouterRegistry.instance = new IORouterRegistry();
- }
- return IORouterRegistry.instance;
- };
- IORouterRegistry.registerSaveRouter = function (saveRouter) {
- IORouterRegistry.getInstance().saveRouters.push(saveRouter);
- };
- IORouterRegistry.registerLoadRouter = function (loadRouter) {
- IORouterRegistry.getInstance().loadRouters.push(loadRouter);
- };
- IORouterRegistry.getSaveHandlers = function (url) {
- return IORouterRegistry.getHandlers(url, 'save');
- };
- IORouterRegistry.getLoadHandlers = function (url) {
- return IORouterRegistry.getHandlers(url, 'load');
- };
- IORouterRegistry.getHandlers = function (url, handlerType) {
- var validHandlers = [];
- var routers = handlerType === 'load' ? this.getInstance().loadRouters :
- this.getInstance().saveRouters;
- routers.forEach(function (router) {
- var handler = router(url);
- if (handler !== null) {
- validHandlers.push(handler);
- }
- });
- return validHandlers;
- };
- return IORouterRegistry;
- }());
-
- var __awaiter$8 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$8 = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var URL_SCHEME_SUFFIX = '://';
- var ModelStoreManagerRegistry = (function () {
- function ModelStoreManagerRegistry() {
- this.managers = {};
- }
- ModelStoreManagerRegistry.getInstance = function () {
- if (ModelStoreManagerRegistry.instance == null) {
- ModelStoreManagerRegistry.instance = new ModelStoreManagerRegistry();
- }
- return ModelStoreManagerRegistry.instance;
- };
- ModelStoreManagerRegistry.registerManager = function (scheme, manager) {
- assert(scheme != null, 'scheme must not be undefined or null.');
- if (scheme.endsWith(URL_SCHEME_SUFFIX)) {
- scheme = scheme.slice(0, scheme.indexOf(URL_SCHEME_SUFFIX));
- }
- assert(scheme.length > 0, 'scheme must not be an empty string.');
- var registry = ModelStoreManagerRegistry.getInstance();
- assert(registry.managers[scheme] == null, "A model store manager is already registered for scheme '" + scheme + "'.");
- registry.managers[scheme] = manager;
- };
- ModelStoreManagerRegistry.getManager = function (scheme) {
- var manager = this.getInstance().managers[scheme];
- if (manager == null) {
- throw new Error("Cannot find model manager for scheme '" + scheme + "'");
- }
- return manager;
- };
- ModelStoreManagerRegistry.getSchemes = function () {
- return Object.keys(this.getInstance().managers);
- };
- return ModelStoreManagerRegistry;
- }());
- function parseURL(url) {
- if (url.indexOf(URL_SCHEME_SUFFIX) === -1) {
- throw new Error("The url string provided does not contain a scheme. " +
- "Supported schemes are: " +
- ("" + ModelStoreManagerRegistry.getSchemes().join(',')));
- }
- return {
- scheme: url.split(URL_SCHEME_SUFFIX)[0],
- path: url.split(URL_SCHEME_SUFFIX)[1],
- };
- }
- function listModels() {
- return __awaiter$8(this, void 0, void 0, function () {
- var schemes, out, _i, schemes_1, scheme, schemeOut, path, url;
- return __generator$8(this, function (_a) {
- switch (_a.label) {
- case 0:
- schemes = ModelStoreManagerRegistry.getSchemes();
- out = {};
- _i = 0, schemes_1 = schemes;
- _a.label = 1;
- case 1:
- if (!(_i < schemes_1.length)) return [3, 4];
- scheme = schemes_1[_i];
- return [4, ModelStoreManagerRegistry.getManager(scheme).listModels()];
- case 2:
- schemeOut = _a.sent();
- for (path in schemeOut) {
- url = scheme + URL_SCHEME_SUFFIX + path;
- out[url] = schemeOut[path];
- }
- _a.label = 3;
- case 3:
- _i++;
- return [3, 1];
- case 4: return [2, out];
- }
- });
- });
- }
- function removeModel(url) {
- return __awaiter$8(this, void 0, void 0, function () {
- var schemeAndPath, manager;
- return __generator$8(this, function (_a) {
- switch (_a.label) {
- case 0:
- schemeAndPath = parseURL(url);
- manager = ModelStoreManagerRegistry.getManager(schemeAndPath.scheme);
- return [4, manager.removeModel(schemeAndPath.path)];
- case 1: return [2, _a.sent()];
- }
- });
- });
- }
- function cloneModelInternal(sourceURL, destURL, deleteSource) {
- if (deleteSource === void 0) { deleteSource = false; }
- return __awaiter$8(this, void 0, void 0, function () {
- var loadHandlers, loadHandler, saveHandlers, saveHandler, sourceScheme, sourcePath, sameMedium, modelArtifacts, saveResult;
- return __generator$8(this, function (_a) {
- switch (_a.label) {
- case 0:
- assert(sourceURL !== destURL, "Old path and new path are the same: '" + sourceURL + "'");
- loadHandlers = IORouterRegistry.getLoadHandlers(sourceURL);
- assert(loadHandlers.length > 0, "Copying failed because no load handler is found for source URL " + sourceURL + ".");
- assert(loadHandlers.length < 2, "Copying failed because more than one (" + loadHandlers.length + ") " +
- ("load handlers for source URL " + sourceURL + "."));
- loadHandler = loadHandlers[0];
- saveHandlers = IORouterRegistry.getSaveHandlers(destURL);
- assert(saveHandlers.length > 0, "Copying failed because no save handler is found for destination URL " +
- (destURL + "."));
- assert(saveHandlers.length < 2, "Copying failed because more than one (" + loadHandlers.length + ") " +
- ("save handlers for destination URL " + destURL + "."));
- saveHandler = saveHandlers[0];
- sourceScheme = parseURL(sourceURL).scheme;
- sourcePath = parseURL(sourceURL).path;
- sameMedium = sourceScheme === parseURL(sourceURL).scheme;
- return [4, loadHandler.load()];
- case 1:
- modelArtifacts = _a.sent();
- if (!(deleteSource && sameMedium)) return [3, 3];
- return [4, ModelStoreManagerRegistry.getManager(sourceScheme)
- .removeModel(sourcePath)];
- case 2:
- _a.sent();
- _a.label = 3;
- case 3: return [4, saveHandler.save(modelArtifacts)];
- case 4:
- saveResult = _a.sent();
- if (!(deleteSource && !sameMedium)) return [3, 6];
- return [4, ModelStoreManagerRegistry.getManager(sourceScheme)
- .removeModel(sourcePath)];
- case 5:
- _a.sent();
- _a.label = 6;
- case 6: return [2, saveResult.modelArtifactsInfo];
- }
- });
- });
- }
- function copyModel(sourceURL, destURL) {
- return __awaiter$8(this, void 0, void 0, function () {
- var deleteSource;
- return __generator$8(this, function (_a) {
- switch (_a.label) {
- case 0:
- deleteSource = false;
- return [4, cloneModelInternal(sourceURL, destURL, deleteSource)];
- case 1: return [2, _a.sent()];
- }
- });
- });
- }
- function moveModel(sourceURL, destURL) {
- return __awaiter$8(this, void 0, void 0, function () {
- var deleteSource;
- return __generator$8(this, function (_a) {
- switch (_a.label) {
- case 0:
- deleteSource = true;
- return [4, cloneModelInternal(sourceURL, destURL, deleteSource)];
- case 1: return [2, _a.sent()];
- }
- });
- });
- }
-
- var __awaiter$9 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$9 = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var DATABASE_NAME = 'tensorflowjs';
- var DATABASE_VERSION = 1;
- var MODEL_STORE_NAME = 'models_store';
- var INFO_STORE_NAME = 'model_info_store';
- function getIndexedDBFactory() {
- if (!ENV.get('IS_BROWSER')) {
- throw new Error('Failed to obtain IndexedDB factory because the current environment' +
- 'is not a web browser.');
- }
- var theWindow = window;
- var factory = theWindow.indexedDB || theWindow.mozIndexedDB ||
- theWindow.webkitIndexedDB || theWindow.msIndexedDB ||
- theWindow.shimIndexedDB;
- if (factory == null) {
- throw new Error('The current browser does not appear to support IndexedDB.');
- }
- return factory;
- }
- function setUpDatabase(openRequest) {
- var db = openRequest.result;
- db.createObjectStore(MODEL_STORE_NAME, { keyPath: 'modelPath' });
- db.createObjectStore(INFO_STORE_NAME, { keyPath: 'modelPath' });
- }
- var BrowserIndexedDB = (function () {
- function BrowserIndexedDB(modelPath) {
- this.indexedDB = getIndexedDBFactory();
- if (modelPath == null || !modelPath) {
- throw new Error('For IndexedDB, modelPath must not be null, undefined or empty.');
- }
- this.modelPath = modelPath;
- }
- BrowserIndexedDB.prototype.save = function (modelArtifacts) {
- return __awaiter$9(this, void 0, void 0, function () {
- return __generator$9(this, function (_a) {
- if (modelArtifacts.modelTopology instanceof ArrayBuffer) {
- throw new Error('BrowserLocalStorage.save() does not support saving model topology ' +
- 'in binary formats yet.');
- }
- return [2, this.databaseAction(this.modelPath, modelArtifacts)];
- });
- });
- };
- BrowserIndexedDB.prototype.load = function () {
- return __awaiter$9(this, void 0, void 0, function () {
- return __generator$9(this, function (_a) {
- return [2, this.databaseAction(this.modelPath)];
- });
- });
- };
- BrowserIndexedDB.prototype.databaseAction = function (modelPath, modelArtifacts) {
- var _this = this;
- return new Promise(function (resolve, reject) {
- var openRequest = _this.indexedDB.open(DATABASE_NAME, DATABASE_VERSION);
- openRequest.onupgradeneeded = function () { return setUpDatabase(openRequest); };
- openRequest.onsuccess = function () {
- var db = openRequest.result;
- if (modelArtifacts == null) {
- var modelTx = db.transaction(MODEL_STORE_NAME, 'readonly');
- var modelStore = modelTx.objectStore(MODEL_STORE_NAME);
- var getRequest_1 = modelStore.get(_this.modelPath);
- getRequest_1.onsuccess = function () {
- if (getRequest_1.result == null) {
- db.close();
- return reject(new Error("Cannot find model with path '" + _this.modelPath + "' " +
- "in IndexedDB."));
- }
- else {
- resolve(getRequest_1.result.modelArtifacts);
- }
- };
- getRequest_1.onerror = function (error) {
- db.close();
- return reject(getRequest_1.error);
- };
- modelTx.oncomplete = function () { return db.close(); };
- }
- else {
- var modelArtifactsInfo_1 = getModelArtifactsInfoForJSON(modelArtifacts);
- var infoTx_1 = db.transaction(INFO_STORE_NAME, 'readwrite');
- var infoStore_1 = infoTx_1.objectStore(INFO_STORE_NAME);
- var putInfoRequest_1 = infoStore_1.put({ modelPath: _this.modelPath, modelArtifactsInfo: modelArtifactsInfo_1 });
- var modelTx_1;
- putInfoRequest_1.onsuccess = function () {
- modelTx_1 = db.transaction(MODEL_STORE_NAME, 'readwrite');
- var modelStore = modelTx_1.objectStore(MODEL_STORE_NAME);
- var putModelRequest = modelStore.put({
- modelPath: _this.modelPath,
- modelArtifacts: modelArtifacts,
- modelArtifactsInfo: modelArtifactsInfo_1
- });
- putModelRequest.onsuccess = function () { return resolve({ modelArtifactsInfo: modelArtifactsInfo_1 }); };
- putModelRequest.onerror = function (error) {
- infoStore_1 = infoTx_1.objectStore(INFO_STORE_NAME);
- var deleteInfoRequest = infoStore_1.delete(_this.modelPath);
- deleteInfoRequest.onsuccess = function () {
- db.close();
- return reject(putModelRequest.error);
- };
- deleteInfoRequest.onerror = function (error) {
- db.close();
- return reject(putModelRequest.error);
- };
- };
- };
- putInfoRequest_1.onerror = function (error) {
- db.close();
- return reject(putInfoRequest_1.error);
- };
- infoTx_1.oncomplete = function () {
- if (modelTx_1 == null) {
- db.close();
- }
- else {
- modelTx_1.oncomplete = function () { return db.close(); };
- }
- };
- }
- };
- openRequest.onerror = function (error) { return reject(openRequest.error); };
- });
- };
- BrowserIndexedDB.URL_SCHEME = 'indexeddb://';
- return BrowserIndexedDB;
- }());
- var indexedDBRouter = function (url) {
- if (!ENV.get('IS_BROWSER')) {
- return null;
- }
- else {
- if (url.startsWith(BrowserIndexedDB.URL_SCHEME)) {
- return browserIndexedDB(url.slice(BrowserIndexedDB.URL_SCHEME.length));
- }
- else {
- return null;
- }
- }
- };
- IORouterRegistry.registerSaveRouter(indexedDBRouter);
- IORouterRegistry.registerLoadRouter(indexedDBRouter);
- function browserIndexedDB(modelPath) {
- return new BrowserIndexedDB(modelPath);
- }
- function maybeStripScheme(key) {
- return key.startsWith(BrowserIndexedDB.URL_SCHEME) ?
- key.slice(BrowserIndexedDB.URL_SCHEME.length) :
- key;
- }
- var BrowserIndexedDBManager = (function () {
- function BrowserIndexedDBManager() {
- this.indexedDB = getIndexedDBFactory();
- }
- BrowserIndexedDBManager.prototype.listModels = function () {
- return __awaiter$9(this, void 0, void 0, function () {
- var _this = this;
- return __generator$9(this, function (_a) {
- return [2, new Promise(function (resolve, reject) {
- var openRequest = _this.indexedDB.open(DATABASE_NAME, DATABASE_VERSION);
- openRequest.onupgradeneeded = function () { return setUpDatabase(openRequest); };
- openRequest.onsuccess = function () {
- var db = openRequest.result;
- var tx = db.transaction(INFO_STORE_NAME, 'readonly');
- var store = tx.objectStore(INFO_STORE_NAME);
- var getAllInfoRequest = store.getAll();
- getAllInfoRequest.onsuccess = function () {
- var out = {};
- for (var _i = 0, _a = getAllInfoRequest.result; _i < _a.length; _i++) {
- var item = _a[_i];
- out[item.modelPath] = item.modelArtifactsInfo;
- }
- resolve(out);
- };
- getAllInfoRequest.onerror = function (error) {
- db.close();
- return reject(getAllInfoRequest.error);
- };
- tx.oncomplete = function () { return db.close(); };
- };
- openRequest.onerror = function (error) { return reject(openRequest.error); };
- })];
- });
- });
- };
- BrowserIndexedDBManager.prototype.removeModel = function (path) {
- return __awaiter$9(this, void 0, void 0, function () {
- var _this = this;
- return __generator$9(this, function (_a) {
- path = maybeStripScheme(path);
- return [2, new Promise(function (resolve, reject) {
- var openRequest = _this.indexedDB.open(DATABASE_NAME, DATABASE_VERSION);
- openRequest.onupgradeneeded = function () { return setUpDatabase(openRequest); };
- openRequest.onsuccess = function () {
- var db = openRequest.result;
- var infoTx = db.transaction(INFO_STORE_NAME, 'readwrite');
- var infoStore = infoTx.objectStore(INFO_STORE_NAME);
- var getInfoRequest = infoStore.get(path);
- var modelTx;
- getInfoRequest.onsuccess = function () {
- if (getInfoRequest.result == null) {
- db.close();
- return reject(new Error("Cannot find model with path '" + path + "' " +
- "in IndexedDB."));
- }
- else {
- var deleteInfoRequest = infoStore.delete(path);
- var deleteModelData_1 = function () {
- modelTx = db.transaction(MODEL_STORE_NAME, 'readwrite');
- var modelStore = modelTx.objectStore(MODEL_STORE_NAME);
- var deleteModelRequest = modelStore.delete(path);
- deleteModelRequest.onsuccess = function () {
- return resolve(getInfoRequest.result.modelArtifactsInfo);
- };
- deleteModelRequest.onerror = function (error) {
- return reject(getInfoRequest.error);
- };
- };
- deleteInfoRequest.onsuccess = deleteModelData_1;
- deleteInfoRequest.onerror = function (error) {
- deleteModelData_1();
- db.close();
- return reject(getInfoRequest.error);
- };
- }
- };
- getInfoRequest.onerror = function (error) {
- db.close();
- return reject(getInfoRequest.error);
- };
- infoTx.oncomplete = function () {
- if (modelTx == null) {
- db.close();
- }
- else {
- modelTx.oncomplete = function () { return db.close(); };
- }
- };
- };
- openRequest.onerror = function (error) { return reject(openRequest.error); };
- })];
- });
- });
- };
- return BrowserIndexedDBManager;
- }());
- if (ENV.get('IS_BROWSER')) {
- try {
- ModelStoreManagerRegistry.registerManager(BrowserIndexedDB.URL_SCHEME, new BrowserIndexedDBManager());
- }
- catch (err) {
- }
- }
-
- var __awaiter$a = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$a = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var PATH_SEPARATOR = '/';
- var PATH_PREFIX = 'tensorflowjs_models';
- var INFO_SUFFIX = 'info';
- var MODEL_TOPOLOGY_SUFFIX = 'model_topology';
- var WEIGHT_SPECS_SUFFIX = 'weight_specs';
- var WEIGHT_DATA_SUFFIX = 'weight_data';
- function getModelKeys(path) {
- return {
- info: [PATH_PREFIX, path, INFO_SUFFIX].join(PATH_SEPARATOR),
- topology: [PATH_PREFIX, path, MODEL_TOPOLOGY_SUFFIX].join(PATH_SEPARATOR),
- weightSpecs: [PATH_PREFIX, path, WEIGHT_SPECS_SUFFIX].join(PATH_SEPARATOR),
- weightData: [PATH_PREFIX, path, WEIGHT_DATA_SUFFIX].join(PATH_SEPARATOR)
- };
- }
- function getModelPathFromKey(key) {
- var items = key.split(PATH_SEPARATOR);
- if (items.length < 3) {
- throw new Error("Invalid key format: " + key);
- }
- return items.slice(1, items.length - 1).join(PATH_SEPARATOR);
- }
- function maybeStripScheme$1(key) {
- return key.startsWith(BrowserLocalStorage.URL_SCHEME) ?
- key.slice(BrowserLocalStorage.URL_SCHEME.length) :
- key;
- }
- var BrowserLocalStorage = (function () {
- function BrowserLocalStorage(modelPath) {
- if (!ENV.get('IS_BROWSER') || typeof window.localStorage === 'undefined') {
- throw new Error('The current environment does not support local storage.');
- }
- this.LS = window.localStorage;
- if (modelPath == null || !modelPath) {
- throw new Error('For local storage, modelPath must not be null, undefined or empty.');
- }
- this.modelPath = modelPath;
- this.keys = getModelKeys(this.modelPath);
- }
- BrowserLocalStorage.prototype.save = function (modelArtifacts) {
- return __awaiter$a(this, void 0, void 0, function () {
- var topology, weightSpecs, modelArtifactsInfo, key;
- return __generator$a(this, function (_a) {
- if (modelArtifacts.modelTopology instanceof ArrayBuffer) {
- throw new Error('BrowserLocalStorage.save() does not support saving model topology ' +
- 'in binary formats yet.');
- }
- else {
- topology = JSON.stringify(modelArtifacts.modelTopology);
- weightSpecs = JSON.stringify(modelArtifacts.weightSpecs);
- modelArtifactsInfo = getModelArtifactsInfoForJSON(modelArtifacts);
- try {
- this.LS.setItem(this.keys.info, JSON.stringify(modelArtifactsInfo));
- this.LS.setItem(this.keys.topology, topology);
- this.LS.setItem(this.keys.weightSpecs, weightSpecs);
- this.LS.setItem(this.keys.weightData, arrayBufferToBase64String(modelArtifacts.weightData));
- return [2, { modelArtifactsInfo: modelArtifactsInfo }];
- }
- catch (err) {
- for (key in this.keys) {
- this.LS.removeItem(this.keys[key]);
- }
- throw new Error("Failed to save model '" + this.modelPath + "' to local storage: " +
- "size quota being exceeded is a possible cause of this failure: " +
- ("modelTopologyBytes=" + modelArtifactsInfo.modelTopologyBytes + ", ") +
- ("weightSpecsBytes=" + modelArtifactsInfo.weightSpecsBytes + ", ") +
- ("weightDataBytes=" + modelArtifactsInfo.weightDataBytes + "."));
- }
- }
- return [2];
- });
- });
- };
- BrowserLocalStorage.prototype.load = function () {
- return __awaiter$a(this, void 0, void 0, function () {
- var info, out, topology, weightSpecs, weightDataBase64;
- return __generator$a(this, function (_a) {
- info = JSON.parse(this.LS.getItem(this.keys.info));
- if (info == null) {
- throw new Error("In local storage, there is no model with name '" + this.modelPath + "'");
- }
- if (info.modelTopologyType !== 'JSON') {
- throw new Error('BrowserLocalStorage does not support loading non-JSON model ' +
- 'topology yet.');
- }
- out = {};
- topology = JSON.parse(this.LS.getItem(this.keys.topology));
- if (topology == null) {
- throw new Error("In local storage, the topology of model '" + this.modelPath + "' " +
- "is missing.");
- }
- out.modelTopology = topology;
- weightSpecs = JSON.parse(this.LS.getItem(this.keys.weightSpecs));
- if (weightSpecs == null) {
- throw new Error("In local storage, the weight specs of model '" + this.modelPath + "' " +
- "are missing.");
- }
- out.weightSpecs = weightSpecs;
- weightDataBase64 = this.LS.getItem(this.keys.weightData);
- if (weightDataBase64 == null) {
- throw new Error("In local storage, the binary weight values of model " +
- ("'" + this.modelPath + "' are missing."));
- }
- out.weightData = base64StringToArrayBuffer(weightDataBase64);
- return [2, out];
- });
- });
- };
- BrowserLocalStorage.URL_SCHEME = 'localstorage://';
- return BrowserLocalStorage;
- }());
- var localStorageRouter = function (url) {
- if (!ENV.get('IS_BROWSER')) {
- return null;
- }
- else {
- if (url.startsWith(BrowserLocalStorage.URL_SCHEME)) {
- return browserLocalStorage(url.slice(BrowserLocalStorage.URL_SCHEME.length));
- }
- else {
- return null;
- }
- }
- };
- IORouterRegistry.registerSaveRouter(localStorageRouter);
- IORouterRegistry.registerLoadRouter(localStorageRouter);
- function browserLocalStorage(modelPath) {
- return new BrowserLocalStorage(modelPath);
- }
- var BrowserLocalStorageManager = (function () {
- function BrowserLocalStorageManager() {
- assert(ENV.get('IS_BROWSER'), 'Current environment is not a web browser');
- assert(typeof window.localStorage !== 'undefined', 'Current browser does not appear to support localStorage');
- this.LS = window.localStorage;
- }
- BrowserLocalStorageManager.prototype.listModels = function () {
- return __awaiter$a(this, void 0, void 0, function () {
- var out, prefix, suffix, i, key, modelPath;
- return __generator$a(this, function (_a) {
- out = {};
- prefix = PATH_PREFIX + PATH_SEPARATOR;
- suffix = PATH_SEPARATOR + INFO_SUFFIX;
- for (i = 0; i < this.LS.length; ++i) {
- key = this.LS.key(i);
- if (key.startsWith(prefix) && key.endsWith(suffix)) {
- modelPath = getModelPathFromKey(key);
- out[modelPath] = JSON.parse(this.LS.getItem(key));
- }
- }
- return [2, out];
- });
- });
- };
- BrowserLocalStorageManager.prototype.removeModel = function (path) {
- return __awaiter$a(this, void 0, void 0, function () {
- var keys, info;
- return __generator$a(this, function (_a) {
- path = maybeStripScheme$1(path);
- keys = getModelKeys(path);
- if (this.LS.getItem(keys.info) == null) {
- throw new Error("Cannot find model at path '" + path + "'");
- }
- info = JSON.parse(this.LS.getItem(keys.info));
- this.LS.removeItem(keys.info);
- this.LS.removeItem(keys.topology);
- this.LS.removeItem(keys.weightSpecs);
- this.LS.removeItem(keys.weightData);
- return [2, info];
- });
- });
- };
- return BrowserLocalStorageManager;
- }());
- if (ENV.get('IS_BROWSER')) {
- try {
- ModelStoreManagerRegistry.registerManager(BrowserLocalStorage.URL_SCHEME, new BrowserLocalStorageManager());
- }
- catch (err) {
- }
- }
-
- var __awaiter$b = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$b = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var DEFAULT_FILE_NAME_PREFIX = 'model';
- var DEFAULT_JSON_EXTENSION_NAME = '.json';
- var DEFAULT_WEIGHT_DATA_EXTENSION_NAME = '.weights.bin';
- var BrowserDownloads = (function () {
- function BrowserDownloads(fileNamePrefix) {
- if (!ENV.get('IS_BROWSER')) {
- throw new Error('triggerDownloads() cannot proceed because the current environment ' +
- 'is not a browser.');
- }
- if (fileNamePrefix.startsWith(BrowserDownloads.URL_SCHEME)) {
- fileNamePrefix = fileNamePrefix.slice(BrowserDownloads.URL_SCHEME.length);
- }
- if (fileNamePrefix == null || fileNamePrefix.length === 0) {
- fileNamePrefix = DEFAULT_FILE_NAME_PREFIX;
- }
- this.modelTopologyFileName = fileNamePrefix + DEFAULT_JSON_EXTENSION_NAME;
- this.weightDataFileName =
- fileNamePrefix + DEFAULT_WEIGHT_DATA_EXTENSION_NAME;
- }
- BrowserDownloads.prototype.save = function (modelArtifacts) {
- return __awaiter$b(this, void 0, void 0, function () {
- var weightsURL, weightsManifest, modelTopologyAndWeightManifest, modelTopologyAndWeightManifestURL, jsonAnchor, weightDataAnchor;
- return __generator$b(this, function (_a) {
- weightsURL = window.URL.createObjectURL(new Blob([modelArtifacts.weightData], { type: 'application/octet-stream' }));
- if (modelArtifacts.modelTopology instanceof ArrayBuffer) {
- throw new Error('DownloadTrigger.save() does not support saving model topology ' +
- 'in binary formats yet.');
- }
- else {
- weightsManifest = [{
- paths: ['./' + this.weightDataFileName],
- weights: modelArtifacts.weightSpecs
- }];
- modelTopologyAndWeightManifest = {
- modelTopology: modelArtifacts.modelTopology,
- weightsManifest: weightsManifest
- };
- modelTopologyAndWeightManifestURL = window.URL.createObjectURL(new Blob([JSON.stringify(modelTopologyAndWeightManifest)], { type: 'application/json' }));
- jsonAnchor = this.jsonAnchor == null ? document.createElement('a') :
- this.jsonAnchor;
- jsonAnchor.download = this.modelTopologyFileName;
- jsonAnchor.href = modelTopologyAndWeightManifestURL;
- jsonAnchor.click();
- if (modelArtifacts.weightData != null) {
- weightDataAnchor = this.weightDataAnchor == null ?
- document.createElement('a') :
- this.weightDataAnchor;
- weightDataAnchor.download = this.weightDataFileName;
- weightDataAnchor.href = weightsURL;
- weightDataAnchor.click();
- }
- return [2, { modelArtifactsInfo: getModelArtifactsInfoForJSON(modelArtifacts) }];
- }
- return [2];
- });
- });
- };
- BrowserDownloads.URL_SCHEME = 'downloads://';
- return BrowserDownloads;
- }());
- var BrowserFiles = (function () {
- function BrowserFiles(files) {
- if (files == null || files.length < 1) {
- throw new Error("When calling browserFiles, at least 1 file is required, " +
- ("but received " + files));
- }
- this.files = files;
- }
- BrowserFiles.prototype.load = function () {
- return __awaiter$b(this, void 0, void 0, function () {
- var _this = this;
- var jsonFile, weightFiles;
- return __generator$b(this, function (_a) {
- jsonFile = this.files[0];
- weightFiles = this.files.slice(1);
- return [2, new Promise(function (resolve, reject) {
- var jsonReader = new FileReader();
- jsonReader.onload = function (event) {
- var modelJSON = JSON.parse(event.target.result);
- var modelTopology = modelJSON.modelTopology;
- if (modelTopology == null) {
- reject(new Error("modelTopology field is missing from file " + jsonFile.name));
- return;
- }
- if (weightFiles.length === 0) {
- resolve({ modelTopology: modelTopology });
- }
- var weightsManifest = modelJSON.weightsManifest;
- if (weightsManifest == null) {
- reject(new Error("weightManifest field is missing from file " + jsonFile.name));
- return;
- }
- var pathToFile;
- try {
- pathToFile =
- _this.checkManifestAndWeightFiles(weightsManifest, weightFiles);
- }
- catch (err) {
- reject(err);
- return;
- }
- var weightSpecs = [];
- var paths = [];
- var perFileBuffers = [];
- weightsManifest.forEach(function (weightsGroup) {
- weightsGroup.paths.forEach(function (path) {
- paths.push(path);
- perFileBuffers.push(null);
- });
- weightSpecs.push.apply(weightSpecs, weightsGroup.weights);
- });
- weightsManifest.forEach(function (weightsGroup) {
- weightsGroup.paths.forEach(function (path) {
- var weightFileReader = new FileReader();
- weightFileReader.onload = function (event) {
- var weightData = event.target.result;
- var index = paths.indexOf(path);
- perFileBuffers[index] = weightData;
- if (perFileBuffers.indexOf(null) === -1) {
- resolve({
- modelTopology: modelTopology,
- weightSpecs: weightSpecs,
- weightData: concatenateArrayBuffers(perFileBuffers),
- });
- }
- };
- weightFileReader.onerror = function (error) {
- reject("Failed to weights data from file of path '" + path + "'.");
- return;
- };
- weightFileReader.readAsArrayBuffer(pathToFile[path]);
- });
- });
- };
- jsonReader.onerror = function (error) {
- reject("Failed to read model topology and weights manifest JSON " +
- ("from file '" + jsonFile.name + "'. BrowserFiles supports loading ") +
- "Keras-style tf.Model artifacts only.");
- return;
- };
- jsonReader.readAsText(jsonFile);
- })];
- });
- });
- };
- BrowserFiles.prototype.checkManifestAndWeightFiles = function (manifest, files) {
- var basenames = [];
- var fileNames = files.map(function (file) { return basename(file.name); });
- var pathToFile = {};
- for (var _i = 0, manifest_1 = manifest; _i < manifest_1.length; _i++) {
- var group = manifest_1[_i];
- group.paths.forEach(function (path) {
- var pathBasename = basename(path);
- if (basenames.indexOf(pathBasename) !== -1) {
- throw new Error("Duplicate file basename found in weights manifest: " +
- ("'" + pathBasename + "'"));
- }
- basenames.push(pathBasename);
- if (fileNames.indexOf(pathBasename) === -1) {
- throw new Error("Weight file with basename '" + pathBasename + "' is not provided.");
- }
- else {
- pathToFile[path] = files[fileNames.indexOf(pathBasename)];
- }
- });
- }
- if (basenames.length !== files.length) {
- throw new Error("Mismatch in the number of files in weights manifest " +
- ("(" + basenames.length + ") and the number of weight files provided ") +
- ("(" + files.length + ")."));
- }
- return pathToFile;
- };
- return BrowserFiles;
- }());
- var browserDownloadsRouter = function (url) {
- if (!ENV.get('IS_BROWSER')) {
- return null;
- }
- else {
- if (url.startsWith(BrowserDownloads.URL_SCHEME)) {
- return browserDownloads(url.slice(BrowserDownloads.URL_SCHEME.length));
- }
- else {
- return null;
- }
- }
- };
- IORouterRegistry.registerSaveRouter(browserDownloadsRouter);
- function browserDownloads(fileNamePrefix) {
- if (fileNamePrefix === void 0) { fileNamePrefix = 'model'; }
- return new BrowserDownloads(fileNamePrefix);
- }
- function browserFiles(files) {
- return new BrowserFiles(files);
- }
-
- var __awaiter$c = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$c = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- var BrowserHTTPRequest = (function () {
- function BrowserHTTPRequest(path, requestInit) {
- this.DEFAULT_METHOD = 'POST';
- if (!ENV.get('IS_BROWSER')) {
- throw new Error('browserHTTPRequest is not supported outside the web browser.');
- }
- assert(path != null && path.length > 0, 'URL path for browserHTTPRequest must not be null, undefined or ' +
- 'empty.');
- this.path = path;
- if (requestInit != null && requestInit.body != null) {
- throw new Error('requestInit is expected to have no pre-existing body, but has one.');
- }
- this.requestInit = requestInit || {};
- }
- BrowserHTTPRequest.prototype.save = function (modelArtifacts) {
- return __awaiter$c(this, void 0, void 0, function () {
- var init, weightsManifest, modelTopologyAndWeightManifest, response;
- return __generator$c(this, function (_a) {
- switch (_a.label) {
- case 0:
- if (modelArtifacts.modelTopology instanceof ArrayBuffer) {
- throw new Error('BrowserHTTPRequest.save() does not support saving model topology ' +
- 'in binary formats yet.');
- }
- init = Object.assign({ method: this.DEFAULT_METHOD }, this.requestInit);
- init.body = new FormData();
- weightsManifest = [{
- paths: ['./model.weights.bin'],
- weights: modelArtifacts.weightSpecs,
- }];
- modelTopologyAndWeightManifest = {
- modelTopology: modelArtifacts.modelTopology,
- weightsManifest: weightsManifest
- };
- init.body.append('model.json', new Blob([JSON.stringify(modelTopologyAndWeightManifest)], { type: 'application/json' }), 'model.json');
- if (modelArtifacts.weightData != null) {
- init.body.append('model.weights.bin', new Blob([modelArtifacts.weightData], { type: 'application/octet-stream' }), 'model.weights.bin');
- }
- return [4, fetch(this.path, init)];
- case 1:
- response = _a.sent();
- if (response.status === 200) {
- return [2, {
- modelArtifactsInfo: getModelArtifactsInfoForJSON(modelArtifacts),
- responses: [response],
- }];
- }
- else {
- throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status " +
- (response.status + "."));
- }
- return [2];
- }
- });
- });
- };
- BrowserHTTPRequest.URL_SCHEMES = ['http://', 'https://'];
- return BrowserHTTPRequest;
- }());
- var httpRequestRouter = function (url) {
- if (!ENV.get('IS_BROWSER')) {
- return null;
- }
- else {
- for (var _i = 0, _a = BrowserHTTPRequest.URL_SCHEMES; _i < _a.length; _i++) {
- var scheme = _a[_i];
- if (url.startsWith(scheme)) {
- return browserHTTPRequest(url);
- }
- }
- return null;
- }
- };
- IORouterRegistry.registerSaveRouter(httpRequestRouter);
- function browserHTTPRequest(path, requestInit) {
- return new BrowserHTTPRequest(path, requestInit);
- }
-
- var __awaiter$d = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step$$1(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step$$1(generator["throw"](value)); } catch (e) { reject(e); } }
- function step$$1(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step$$1((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator$d = (undefined && undefined.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step$$1([n, v]); }; }
- function step$$1(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- function loadWeights(manifest, filePathPrefix, weightNames, requestOptions) {
- if (filePathPrefix === void 0) { filePathPrefix = ''; }
- return __awaiter$d(this, void 0, void 0, function () {
- var groupIndicesToFetchMap, groupWeightsToFetch, weightsFound, allManifestWeightNames, weightsNotFound, groupIndicesToFetch, requests, responses, buffers, weightsTensorMap, bufferIndexOffset;
- return __generator$d(this, function (_a) {
- switch (_a.label) {
- case 0:
- groupIndicesToFetchMap = manifest.map(function () { return false; });
- groupWeightsToFetch = {};
- weightsFound = weightNames != null ? weightNames.map(function () { return false; }) : [];
- allManifestWeightNames = [];
- manifest.forEach(function (manifestGroupConfig, groupIndex) {
- var groupOffset = 0;
- manifestGroupConfig.weights.forEach(function (weightsEntry) {
- var rawDtype = ('quantization' in weightsEntry) ?
- weightsEntry.quantization.dtype :
- weightsEntry.dtype;
- var weightsBytes = DTYPE_VALUE_SIZE_MAP[rawDtype] *
- sizeFromShape(weightsEntry.shape);
- var enqueueWeightsForFetchingFn = function () {
- groupIndicesToFetchMap[groupIndex] = true;
- if (groupWeightsToFetch[groupIndex] == null) {
- groupWeightsToFetch[groupIndex] = [];
- }
- groupWeightsToFetch[groupIndex].push({
- manifestEntry: weightsEntry,
- groupOffset: groupOffset,
- sizeBytes: weightsBytes
- });
- };
- if (weightNames != null) {
- weightNames.forEach(function (weightName, weightIndex) {
- if (weightName === weightsEntry.name) {
- enqueueWeightsForFetchingFn();
- weightsFound[weightIndex] = true;
- }
- });
- }
- else {
- enqueueWeightsForFetchingFn();
- }
- allManifestWeightNames.push(weightsEntry.name);
- groupOffset += weightsBytes;
- });
- });
- if (!weightsFound.every(function (found) { return found; })) {
- weightsNotFound = weightNames.filter(function (weight, i) { return !weightsFound[i]; });
- throw new Error("Could not find weights in manifest with names: " +
- (weightsNotFound.join(', ') + ". \n") +
- "Manifest JSON has weights with names: " +
- (allManifestWeightNames.join(', ') + "."));
- }
- groupIndicesToFetch = groupIndicesToFetchMap.reduce(function (accumulator, shouldFetch, i) {
- if (shouldFetch) {
- accumulator.push(i);
- }
- return accumulator;
- }, []);
- requests = [];
- groupIndicesToFetch.forEach(function (i) {
- manifest[i].paths.forEach(function (filepath) {
- var fetchUrl = filePathPrefix +
- (!filePathPrefix.endsWith('/') ? '/' : '') + filepath;
- requests.push(fetch(fetchUrl, requestOptions));
- });
- });
- return [4, Promise.all(requests)];
- case 1:
- responses = _a.sent();
- return [4, Promise.all(responses.map(function (response) { return response.arrayBuffer(); }))];
- case 2:
- buffers = _a.sent();
- weightsTensorMap = {};
- bufferIndexOffset = 0;
- groupIndicesToFetch.forEach(function (i) {
- var numBuffers = manifest[i].paths.length;
- var groupBytes = 0;
- for (var i_1 = 0; i_1 < numBuffers; i_1++) {
- groupBytes += buffers[bufferIndexOffset + i_1].byteLength;
- }
- var groupBuffer = new ArrayBuffer(groupBytes);
- var groupByteBuffer = new Uint8Array(groupBuffer);
- var groupBufferOffset = 0;
- for (var i_2 = 0; i_2 < numBuffers; i_2++) {
- var buffer$$1 = new Uint8Array(buffers[bufferIndexOffset + i_2]);
- groupByteBuffer.set(buffer$$1, groupBufferOffset);
- groupBufferOffset += buffer$$1.byteLength;
- }
- var weightsEntries = groupWeightsToFetch[i];
- weightsEntries.forEach(function (weightsEntry) {
- var byteBuffer = groupBuffer.slice(weightsEntry.groupOffset, weightsEntry.groupOffset + weightsEntry.sizeBytes);
- var typedArray;
- var dtype = weightsEntry.manifestEntry.dtype;
- if ('quantization' in weightsEntry.manifestEntry) {
- var quantization_1 = weightsEntry.manifestEntry.quantization;
- if (quantization_1.dtype !== 'uint8' && quantization_1.dtype !== 'uint16') {
- throw new Error("Weight " + weightsEntry.manifestEntry.name + " has unknown " +
- ("quantization dtype " + quantization_1.dtype + "."));
- }
- var quantizedArray = (quantization_1.dtype === 'uint8') ?
- new Uint8Array(byteBuffer) :
- new Uint16Array(byteBuffer);
- if (dtype === 'float32') {
- typedArray = Float32Array.from(quantizedArray, function (v) { return v * quantization_1.scale + quantization_1.min; });
- }
- else if (dtype === 'int32') {
- typedArray = Int32Array.from(quantizedArray, function (v) { return Math.round(v * quantization_1.scale + quantization_1.min); });
- }
- else {
- throw new Error("Weight " + weightsEntry.manifestEntry.name + " has a dtype not " +
- ("supported by quantization: " + dtype));
- }
- }
- else {
- if (dtype === 'float32') {
- typedArray = new Float32Array(byteBuffer);
- }
- else if (dtype === 'int32') {
- typedArray = new Int32Array(byteBuffer);
- }
- else {
- throw new Error("Weight " + weightsEntry.manifestEntry.name + " has unknown dtype " +
- (dtype + "."));
- }
- }
- var weightName = weightsEntry.manifestEntry.name;
- if (weightsTensorMap[weightName] != null) {
- throw new Error("Duplicate weight with name " + weightName + ". " +
- "Please make sure weights names are unique in the manifest JSON.");
- }
- weightsTensorMap[weightName] = tensor(typedArray, weightsEntry.manifestEntry.shape, weightsEntry.manifestEntry.dtype);
- });
- bufferIndexOffset += numBuffers;
- });
- return [2, weightsTensorMap];
- }
- });
- });
- }
-
- var registerSaveRouter = IORouterRegistry.registerSaveRouter;
- var registerLoadRouter = IORouterRegistry.registerLoadRouter;
- var getSaveHandlers = IORouterRegistry.getSaveHandlers;
- var getLoadHandlers = IORouterRegistry.getLoadHandlers;
-
- var io = /*#__PURE__*/Object.freeze({
- browserFiles: browserFiles,
- browserHTTPRequest: browserHTTPRequest,
- copyModel: copyModel,
- decodeWeights: decodeWeights,
- encodeWeights: encodeWeights,
- getLoadHandlers: getLoadHandlers,
- getSaveHandlers: getSaveHandlers,
- listModels: listModels,
- loadWeights: loadWeights,
- moveModel: moveModel,
- registerLoadRouter: registerLoadRouter,
- registerSaveRouter: registerSaveRouter,
- removeModel: removeModel
- });
-
- var Serializable = (function () {
- function Serializable() {
- }
- Serializable.prototype.getClassName = function () {
- return this.constructor
- .className;
- };
- Serializable.fromConfig = function (cls, config) {
- return new cls(config);
- };
- return Serializable;
- }());
- var SerializationMap = (function () {
- function SerializationMap() {
- this.classNameMap = {};
- }
- SerializationMap.getMap = function () {
- if (SerializationMap.instance == null) {
- SerializationMap.instance = new SerializationMap();
- }
- return SerializationMap.instance;
- };
- SerializationMap.register = function (cls) {
- this.getMap().classNameMap[cls.className] = [cls, cls.fromConfig];
- };
- return SerializationMap;
- }());
-
- var serialization = /*#__PURE__*/Object.freeze({
- Serializable: Serializable,
- SerializationMap: SerializationMap
- });
-
- var WEBGL_ENVS = {
- 'BACKEND': 'test-webgl'
- };
- var CPU_ENVS = {
- 'BACKEND': 'test-cpu'
- };
- var ALL_ENVS = {};
- var TEST_EPSILON = 1e-3;
- function expectArraysClose(actual, expected, epsilon) {
- if (epsilon === void 0) { epsilon = TEST_EPSILON; }
- if (!(actual instanceof Tensor) && !(expected instanceof Tensor)) {
- var aType = actual.constructor.name;
- var bType = expected.constructor.name;
- if (aType !== bType) {
- throw new Error("Arrays are of different type actual: " + aType + " " +
- ("vs expected: " + bType));
- }
- }
- else if (actual instanceof Tensor && expected instanceof Tensor) {
- if (actual.dtype !== expected.dtype) {
- throw new Error("Arrays are of different type actual: " + actual.dtype + " " +
- ("vs expected: " + expected.dtype + "."));
- }
- if (!arraysEqual(actual.shape, expected.shape)) {
- throw new Error("Arrays are of different shape actual: " + actual.shape + " " +
- ("vs expected: " + expected.shape + "."));
- }
- }
- var actualValues;
- var expectedValues;
- if (actual instanceof Tensor) {
- actualValues = actual.dataSync();
- }
- else {
- actualValues = actual;
- }
- if (expected instanceof Tensor) {
- expectedValues = expected.dataSync();
- }
- else {
- expectedValues = expected;
- }
- if (actualValues.length !== expectedValues.length) {
- throw new Error("Arrays have different lengths actual: " + actualValues.length + " vs " +
- ("expected: " + expectedValues.length + ".\n") +
- ("Actual: " + actualValues + ".\n") +
- ("Expected: " + expectedValues + "."));
- }
- for (var i = 0; i < expectedValues.length; ++i) {
- var a = actualValues[i];
- var e = expectedValues[i];
- if (!areClose(a, Number(e), epsilon)) {
- throw new Error("Arrays differ: actual[" + i + "] = " + a + ", expected[" + i + "] = " + e + ".\n" +
- ("Actual: " + actualValues + ".\n") +
- ("Expected: " + expectedValues + "."));
- }
- }
- }
- function expectPromiseToFail(fn, done) {
- fn().then(function () { return done.fail(); }, function () { return done(); });
- }
- function expectArraysEqual(actual, expected) {
- return expectArraysClose(actual, expected, 0);
- }
- function expectNumbersClose(a, e, epsilon) {
- if (epsilon === void 0) { epsilon = TEST_EPSILON; }
- if (!areClose(a, e, epsilon)) {
- throw new Error("Numbers differ: actual === " + a + ", expected === " + e);
- }
- }
- function areClose(a, e, epsilon) {
- if (isNaN(a) && isNaN(e)) {
- return true;
- }
- if (isNaN(a) || isNaN(e) || Math.abs(a - e) > epsilon) {
- return false;
- }
- return true;
- }
- function expectValuesInRange(actual, low, high) {
- var actualVals;
- if (actual instanceof Tensor) {
- actualVals = actual.dataSync();
- }
- else {
- actualVals = actual;
- }
- for (var i = 0; i < actualVals.length; i++) {
- if (actualVals[i] < low || actualVals[i] > high) {
- throw new Error("Value out of range:" + actualVals[i] + " low: " + low + ", high: " + high);
- }
- }
- }
-
- var test_util = /*#__PURE__*/Object.freeze({
- WEBGL_ENVS: WEBGL_ENVS,
- CPU_ENVS: CPU_ENVS,
- ALL_ENVS: ALL_ENVS,
- TEST_EPSILON: TEST_EPSILON,
- expectArraysClose: expectArraysClose,
- expectPromiseToFail: expectPromiseToFail,
- expectArraysEqual: expectArraysEqual,
- expectNumbersClose: expectNumbersClose,
- expectValuesInRange: expectValuesInRange
- });
-
- var version = '0.11.0';
-
-
-
- var webgl = /*#__PURE__*/Object.freeze({
- gpgpu_util: gpgpu_util,
- webgl_util: webgl_util,
- MathBackendWebGL: MathBackendWebGL,
- GPGPUContext: GPGPUContext
- });
-
- var __extends$1 = (undefined && undefined.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- var __decorate$s = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var Optimizer = (function (_super) {
- __extends$1(Optimizer, _super);
- function Optimizer() {
- return _super !== null && _super.apply(this, arguments) || this;
- }
- Optimizer.prototype.minimize = function (f, returnCost, varList) {
- if (returnCost === void 0) { returnCost = false; }
- var _a = this.computeGradients(f, varList), value = _a.value, grads$$1 = _a.grads;
- this.applyGradients(grads$$1);
- var varNames = Object.keys(grads$$1);
- varNames.forEach(function (varName) { return grads$$1[varName].dispose(); });
- if (returnCost) {
- return value;
- }
- else {
- value.dispose();
- return null;
- }
- };
- Optimizer.prototype.computeGradients = function (f, varList) {
- return variableGrads(f, varList);
- };
- __decorate$s([
- doc({ heading: 'Training', subheading: 'Optimizers' })
- ], Optimizer.prototype, "minimize", null);
- Optimizer = __decorate$s([
- doc({ heading: 'Training', subheading: 'Classes', namespace: 'train' })
- ], Optimizer);
- return Optimizer;
- }(Serializable));
-
- var __extends$2 = (undefined && undefined.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- var AdadeltaOptimizer = (function (_super) {
- __extends$2(AdadeltaOptimizer, _super);
- function AdadeltaOptimizer(learningRate, rho, epsilon) {
- if (epsilon === void 0) { epsilon = 1e-8; }
- var _this = _super.call(this) || this;
- _this.learningRate = learningRate;
- _this.rho = rho;
- _this.epsilon = epsilon;
- _this.accumulatedGrads = {};
- _this.accumulatedUpdates = {};
- _this.c = keep(scalar(-learningRate));
- _this.epsilonScalar = keep(scalar(epsilon));
- _this.rhoScalar = keep(scalar(rho));
- _this.oneMinusRho = keep(scalar(1 - rho));
- return _this;
- }
- AdadeltaOptimizer.prototype.applyGradients = function (variableGradients) {
- var _this = this;
- var _loop_1 = function (variableName) {
- var value = ENV.engine.registeredVariables[variableName];
- if (this_1.accumulatedGrads[variableName] == null) {
- var trainable_1 = false;
- tidy(function () {
- _this.accumulatedGrads[variableName] =
- zerosLike(value).variable(trainable_1);
- });
- }
- if (this_1.accumulatedUpdates[variableName] == null) {
- var trainable_2 = false;
- tidy(function () {
- _this.accumulatedUpdates[variableName] =
- zerosLike(value).variable(trainable_2);
- });
- }
- var gradient = variableGradients[variableName];
- var accumulatedGrad = this_1.accumulatedGrads[variableName];
- var accumulatedUpdate = this_1.accumulatedUpdates[variableName];
- tidy(function () {
- var newAccumulatedGrad = _this.rhoScalar.mul(accumulatedGrad)
- .add(_this.oneMinusRho.mul(gradient.square()));
- var updates = accumulatedUpdate.add(_this.epsilonScalar)
- .sqrt()
- .div(accumulatedGrad.add(_this.epsilonScalar).sqrt())
- .mul(gradient);
- var newAccumulatedUpdate = _this.rhoScalar.mul(accumulatedUpdate)
- .add(_this.oneMinusRho.mul(updates.square()));
- _this.accumulatedGrads[variableName].assign(newAccumulatedGrad);
- _this.accumulatedUpdates[variableName].assign(newAccumulatedUpdate);
- var newValue = _this.c.mul(updates).add(value);
- value.assign(newValue);
- });
- };
- var this_1 = this;
- for (var variableName in variableGradients) {
- _loop_1(variableName);
- }
- };
- AdadeltaOptimizer.prototype.dispose = function () {
- var _this = this;
- this.c.dispose();
- this.epsilonScalar.dispose();
- this.rhoScalar.dispose();
- this.oneMinusRho.dispose();
- if (this.accumulatedUpdates != null) {
- Object.keys(this.accumulatedUpdates)
- .forEach(function (name) { return _this.accumulatedUpdates[name].dispose(); });
- Object.keys(this.accumulatedGrads)
- .forEach(function (name) { return _this.accumulatedGrads[name].dispose(); });
- }
- };
- AdadeltaOptimizer.prototype.getConfig = function () {
- return {
- learningRate: this.learningRate,
- rho: this.rho,
- epsilon: this.epsilon
- };
- };
- AdadeltaOptimizer.fromConfig = function (cls, config) {
- return new cls(config.learningRate, config.rho, config.epsilon);
- };
- AdadeltaOptimizer.className = 'AdadeltaOptimizer';
- return AdadeltaOptimizer;
- }(Optimizer));
- SerializationMap.register(AdadeltaOptimizer);
-
- var __extends$3 = (undefined && undefined.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- var AdagradOptimizer = (function (_super) {
- __extends$3(AdagradOptimizer, _super);
- function AdagradOptimizer(learningRate, initialAccumulatorValue) {
- if (initialAccumulatorValue === void 0) { initialAccumulatorValue = 0.1; }
- var _this = _super.call(this) || this;
- _this.learningRate = learningRate;
- _this.initialAccumulatorValue = initialAccumulatorValue;
- _this.accumulatedGrads = {};
- _this.c = keep(scalar(-learningRate));
- _this.epsilon = keep(scalar(1e-8));
- return _this;
- }
- AdagradOptimizer.prototype.applyGradients = function (variableGradients) {
- var _this = this;
- var _loop_1 = function (variableName) {
- var value = ENV.engine.registeredVariables[variableName];
- if (this_1.accumulatedGrads[variableName] == null) {
- var trainable_1 = false;
- tidy(function () {
- _this.accumulatedGrads[variableName] =
- fill(value.shape, _this.initialAccumulatorValue)
- .variable(trainable_1);
- });
- }
- var gradient = variableGradients[variableName];
- var accumulatedGrad = this_1.accumulatedGrads[variableName];
- tidy(function () {
- var newAccumulatedGrad = accumulatedGrad.add(gradient.square());
- _this.accumulatedGrads[variableName].assign(newAccumulatedGrad);
- var newValue = _this.c
- .mul(gradient.div(newAccumulatedGrad.add(_this.epsilon).sqrt()))
- .add(value);
- value.assign(newValue);
- });
- };
- var this_1 = this;
- for (var variableName in variableGradients) {
- _loop_1(variableName);
- }
- };
- AdagradOptimizer.prototype.dispose = function () {
- var _this = this;
- this.epsilon.dispose();
- this.c.dispose();
- if (this.accumulatedGrads != null) {
- Object.keys(this.accumulatedGrads)
- .forEach(function (name) { return _this.accumulatedGrads[name].dispose(); });
- }
- };
- AdagradOptimizer.prototype.getConfig = function () {
- return {
- learningRate: this.learningRate,
- initialAccumulatorValue: this.initialAccumulatorValue,
- };
- };
- AdagradOptimizer.fromConfig = function (cls, config) {
- return new cls(config.learningRate, config.initialAccumulatorValue);
- };
- AdagradOptimizer.className = 'AdagradOptimizer';
- return AdagradOptimizer;
- }(Optimizer));
- SerializationMap.register(AdagradOptimizer);
-
- var __extends$4 = (undefined && undefined.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- var AdamOptimizer = (function (_super) {
- __extends$4(AdamOptimizer, _super);
- function AdamOptimizer(learningRate, beta1, beta2, epsilon) {
- if (epsilon === void 0) { epsilon = 1e-8; }
- var _this = _super.call(this) || this;
- _this.learningRate = learningRate;
- _this.beta1 = beta1;
- _this.beta2 = beta2;
- _this.epsilon = epsilon;
- _this.accumulatedFirstMoment = {};
- _this.accumulatedSecondMoment = {};
- _this.c = keep(scalar(-learningRate));
- _this.epsScalar = keep(scalar(epsilon));
- _this.beta1Scalar = keep(scalar(beta1));
- _this.beta2Scalar = keep(scalar(beta2));
- tidy(function () {
- _this.accBeta1 = scalar(beta1).variable();
- _this.accBeta2 = scalar(beta2).variable();
- });
- _this.oneMinusBeta1 = keep(scalar(1 - beta1));
- _this.oneMinusBeta2 = keep(scalar(1 - beta2));
- _this.one = keep(scalar(1));
- return _this;
- }
- AdamOptimizer.prototype.applyGradients = function (variableGradients) {
- var _this = this;
- tidy(function () {
- var oneMinusAccBeta1 = _this.one.sub(_this.accBeta1);
- var oneMinusAccBeta2 = _this.one.sub(_this.accBeta2);
- for (var variableName in variableGradients) {
- var value = ENV.engine.registeredVariables[variableName];
- if (_this.accumulatedFirstMoment[variableName] == null) {
- var trainable = false;
- _this.accumulatedFirstMoment[variableName] =
- zerosLike(value).variable(trainable);
- }
- if (_this.accumulatedSecondMoment[variableName] == null) {
- var trainable = false;
- _this.accumulatedSecondMoment[variableName] =
- zerosLike(value).variable(trainable);
- }
- var gradient = variableGradients[variableName];
- var firstMoment = _this.accumulatedFirstMoment[variableName];
- var secondMoment = _this.accumulatedSecondMoment[variableName];
- var newFirstMoment = _this.beta1Scalar.mul(firstMoment)
- .add(_this.oneMinusBeta1.mul(gradient));
- var newSecondMoment = _this.beta2Scalar.mul(secondMoment)
- .add(_this.oneMinusBeta2.mul(gradient.square()));
- var biasCorrectedFirstMoment = newFirstMoment.div(oneMinusAccBeta1);
- var biasCorrectedSecondMoment = newSecondMoment.div(oneMinusAccBeta2);
- _this.accumulatedFirstMoment[variableName].assign(newFirstMoment);
- _this.accumulatedSecondMoment[variableName].assign(newSecondMoment);
- var newValue = _this.c
- .mul(biasCorrectedFirstMoment.div(_this.epsScalar.add(biasCorrectedSecondMoment.sqrt())))
- .add(value);
- value.assign(newValue);
- }
- _this.accBeta1.assign(_this.accBeta1.mul(_this.beta1Scalar));
- _this.accBeta2.assign(_this.accBeta2.mul(_this.beta2Scalar));
- });
- };
- AdamOptimizer.prototype.dispose = function () {
- var _this = this;
- this.c.dispose();
- this.epsScalar.dispose();
- this.beta1Scalar.dispose();
- this.beta2Scalar.dispose();
- this.accBeta1.dispose();
- this.accBeta2.dispose();
- this.oneMinusBeta1.dispose();
- this.oneMinusBeta2.dispose();
- this.one.dispose();
- if (this.accumulatedFirstMoment != null) {
- Object.keys(this.accumulatedFirstMoment)
- .forEach(function (name) { return _this.accumulatedFirstMoment[name].dispose(); });
- }
- if (this.accumulatedSecondMoment != null) {
- Object.keys(this.accumulatedSecondMoment)
- .forEach(function (name) { return _this.accumulatedSecondMoment[name].dispose(); });
- }
- };
- AdamOptimizer.prototype.getConfig = function () {
- return {
- learningRate: this.learningRate,
- beta1: this.beta1,
- beta2: this.beta2,
- epsilon: this.epsilon,
- };
- };
- AdamOptimizer.fromConfig = function (cls, config) {
- return new cls(config.learningRate, config.beta1, config.beta2, config.epsilon);
- };
- AdamOptimizer.className = 'AdamOptimizer';
- return AdamOptimizer;
- }(Optimizer));
- SerializationMap.register(AdamOptimizer);
-
- var __extends$5 = (undefined && undefined.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- var AdamaxOptimizer = (function (_super) {
- __extends$5(AdamaxOptimizer, _super);
- function AdamaxOptimizer(learningRate, beta1, beta2, epsilon, decay) {
- if (epsilon === void 0) { epsilon = 1e-8; }
- if (decay === void 0) { decay = 0.0; }
- var _this = _super.call(this) || this;
- _this.learningRate = learningRate;
- _this.beta1 = beta1;
- _this.beta2 = beta2;
- _this.epsilon = epsilon;
- _this.decay = decay;
- _this.accumulatedFirstMoment = {};
- _this.accumulatedWeightedInfNorm = {};
- _this.c = keep(scalar(-learningRate));
- _this.epsScalar = keep(scalar(epsilon));
- _this.beta1Scalar = keep(scalar(beta1));
- _this.beta2Scalar = keep(scalar(beta2));
- _this.decayScalar = keep(scalar(decay));
- tidy(function () {
- _this.iteration = scalar(0).variable();
- _this.accBeta1 = scalar(beta1).variable();
- });
- _this.oneMinusBeta1 = keep(scalar(1 - beta1));
- _this.one = keep(scalar(1));
- return _this;
- }
- AdamaxOptimizer.prototype.applyGradients = function (variableGradients) {
- var _this = this;
- tidy(function () {
- var oneMinusAccBeta1 = _this.one.sub(_this.accBeta1);
- var lr = _this.c.div(_this.one.add(_this.decayScalar.mul(_this.iteration)));
- for (var variableName in variableGradients) {
- var value = ENV.engine.registeredVariables[variableName];
- if (_this.accumulatedFirstMoment[variableName] == null) {
- var trainable = false;
- _this.accumulatedFirstMoment[variableName] =
- zerosLike(value).variable(trainable);
- }
- if (_this.accumulatedWeightedInfNorm[variableName] == null) {
- var trainable = false;
- _this.accumulatedWeightedInfNorm[variableName] =
- zerosLike(value).variable(trainable);
- }
- var gradient = variableGradients[variableName];
- var firstMoment = _this.accumulatedFirstMoment[variableName];
- var weightedInfNorm = _this.accumulatedWeightedInfNorm[variableName];
- var newFirstMoment = _this.beta1Scalar.mul(firstMoment)
- .add(_this.oneMinusBeta1.mul(gradient));
- var ut0 = _this.beta2Scalar.mul(weightedInfNorm);
- var ut1 = gradient.abs();
- var newWeightedInfNorm = ut0.maximum(ut1);
- _this.accumulatedFirstMoment[variableName].assign(newFirstMoment);
- _this.accumulatedWeightedInfNorm[variableName].assign(newWeightedInfNorm);
- var newValue = lr.div(oneMinusAccBeta1)
- .mul(newFirstMoment.div(_this.epsScalar.add(newWeightedInfNorm)))
- .add(value);
- value.assign(newValue);
- }
- _this.iteration.assign(_this.iteration.add(_this.one));
- _this.accBeta1.assign(_this.accBeta1.mul(_this.beta1Scalar));
- });
- };
- AdamaxOptimizer.prototype.dispose = function () {
- var _this = this;
- this.c.dispose();
- this.epsScalar.dispose();
- this.accBeta1.dispose();
- this.beta1Scalar.dispose();
- this.beta2Scalar.dispose();
- this.oneMinusBeta1.dispose();
- this.decayScalar.dispose();
- this.iteration.dispose();
- this.one.dispose();
- if (this.accumulatedFirstMoment != null) {
- Object.keys(this.accumulatedFirstMoment)
- .forEach(function (name) { return _this.accumulatedFirstMoment[name].dispose(); });
- }
- if (this.accumulatedWeightedInfNorm != null) {
- Object.keys(this.accumulatedWeightedInfNorm)
- .forEach(function (name) { return _this.accumulatedWeightedInfNorm[name].dispose(); });
- }
- };
- AdamaxOptimizer.prototype.getConfig = function () {
- return {
- learningRate: this.learningRate,
- beta1: this.beta1,
- beta2: this.beta2,
- epsilon: this.epsilon,
- decay: this.decay
- };
- };
- AdamaxOptimizer.fromConfig = function (cls, config) {
- return new cls(config.learningRate, config.beta1, config.beta2, config.epsilon, config.decay);
- };
- AdamaxOptimizer.className = 'AdamaxOptimizer';
- return AdamaxOptimizer;
- }(Optimizer));
- SerializationMap.register(AdamaxOptimizer);
-
- var __extends$6 = (undefined && undefined.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- var SGDOptimizer = (function (_super) {
- __extends$6(SGDOptimizer, _super);
- function SGDOptimizer(learningRate) {
- var _this = _super.call(this) || this;
- _this.learningRate = learningRate;
- _this.setLearningRate(learningRate);
- return _this;
- }
- SGDOptimizer.prototype.applyGradients = function (variableGradients) {
- var _this = this;
- var varNames = Object.keys(variableGradients);
- varNames.forEach(function (varName) {
- var gradient = variableGradients[varName];
- var value = ENV.engine.registeredVariables[varName];
- tidy(function () {
- var newValue = _this.c.mul(gradient).add(value);
- value.assign(newValue);
- });
- });
- };
- SGDOptimizer.prototype.setLearningRate = function (learningRate) {
- this.learningRate = learningRate;
- if (this.c != null) {
- this.c.dispose();
- }
- this.c = keep(scalar(-learningRate));
- };
- SGDOptimizer.prototype.dispose = function () {
- this.c.dispose();
- };
- SGDOptimizer.prototype.getConfig = function () {
- return { learningRate: this.learningRate };
- };
- SGDOptimizer.fromConfig = function (cls, config) {
- return new cls(config.learningRate);
- };
- SGDOptimizer.className = 'SGDOptimizer';
- return SGDOptimizer;
- }(Optimizer));
- SerializationMap.register(SGDOptimizer);
-
- var __extends$7 = (undefined && undefined.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- var MomentumOptimizer = (function (_super) {
- __extends$7(MomentumOptimizer, _super);
- function MomentumOptimizer(learningRate, momentum, useNesterov) {
- if (useNesterov === void 0) { useNesterov = false; }
- var _this = _super.call(this, learningRate) || this;
- _this.learningRate = learningRate;
- _this.momentum = momentum;
- _this.useNesterov = useNesterov;
- _this.m = scalar(_this.momentum);
- _this.accumulations = {};
- return _this;
- }
- MomentumOptimizer.prototype.applyGradients = function (variableGradients) {
- var _this = this;
- var _loop_1 = function (variableName) {
- var value = ENV.engine.registeredVariables[variableName];
- if (this_1.accumulations[variableName] == null) {
- var trainable_1 = false;
- tidy(function () {
- _this.accumulations[variableName] =
- zerosLike(value).variable(trainable_1);
- });
- }
- var accumulation = this_1.accumulations[variableName];
- var gradient = variableGradients[variableName];
- tidy(function () {
- var newValue;
- var newAccumulation = _this.m.mul(accumulation).add(gradient);
- if (_this.useNesterov) {
- newValue =
- _this.c.mul(gradient.add(newAccumulation.mul(_this.m))).add(value);
- }
- else {
- newValue = _this.c.mul(newAccumulation).add(value);
- }
- _this.accumulations[variableName].assign(newAccumulation);
- value.assign(newValue);
- });
- };
- var this_1 = this;
- for (var variableName in variableGradients) {
- _loop_1(variableName);
- }
- };
- MomentumOptimizer.prototype.dispose = function () {
- _super.prototype.dispose.call(this);
- this.m.dispose();
- if (this.accumulations != null) {
- for (var variableName in this.accumulations) {
- this.accumulations[variableName].dispose();
- }
- }
- };
- MomentumOptimizer.prototype.setMomentum = function (momentum) {
- this.momentum = momentum;
- };
- MomentumOptimizer.prototype.getConfig = function () {
- return {
- learningRate: this.learningRate,
- momentum: this.momentum,
- useNesterov: this.useNesterov
- };
- };
- MomentumOptimizer.fromConfig = function (cls, config) {
- return new cls(config.learningRate, config.momentum, config.useNesterov);
- };
- MomentumOptimizer.className = 'MomentumOptimizer';
- return MomentumOptimizer;
- }(SGDOptimizer));
- SerializationMap.register(MomentumOptimizer);
-
- var __extends$8 = (undefined && undefined.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- var RMSPropOptimizer = (function (_super) {
- __extends$8(RMSPropOptimizer, _super);
- function RMSPropOptimizer(learningRate, decay, momentum, epsilon, centered) {
- if (decay === void 0) { decay = 0.9; }
- if (momentum === void 0) { momentum = 0.0; }
- if (epsilon === void 0) { epsilon = 1e-8; }
- if (centered === void 0) { centered = false; }
- var _this = _super.call(this) || this;
- _this.learningRate = learningRate;
- _this.decay = decay;
- _this.momentum = momentum;
- _this.epsilon = epsilon;
- _this.accumulatedMeanSquares = {};
- _this.accumulatedMeanGrads = {};
- _this.accumulatedMoments = {};
- _this.c = keep(scalar(learningRate));
- _this.epsilonScalar = keep(scalar(epsilon));
- _this.decayScalar = keep(scalar(decay));
- _this.momentumScalar = keep(scalar(momentum));
- _this.oneMinusDecay = keep(scalar(1 - decay));
- _this.centered = centered;
- return _this;
- }
- RMSPropOptimizer.prototype.applyGradients = function (variableGradients) {
- var _this = this;
- var _loop_1 = function (variableName) {
- var value = ENV.engine.registeredVariables[variableName];
- if (this_1.accumulatedMeanSquares[variableName] == null) {
- var trainable_1 = false;
- tidy(function () {
- _this.accumulatedMeanSquares[variableName] =
- zerosLike(value).variable(trainable_1);
- });
- }
- if (this_1.accumulatedMeanGrads[variableName] == null && this_1.centered) {
- var trainable_2 = false;
- tidy(function () {
- _this.accumulatedMeanGrads[variableName] =
- zerosLike(value).variable(trainable_2);
- });
- }
- if (this_1.accumulatedMoments[variableName] == null) {
- var trainable_3 = false;
- tidy(function () {
- _this.accumulatedMoments[variableName] =
- zerosLike(value).variable(trainable_3);
- });
- }
- var accumulatedMeanSquare = this_1.accumulatedMeanSquares[variableName];
- var accumulatedMeanGrad = this_1.accumulatedMeanGrads[variableName];
- var accumulatedMoments = this_1.accumulatedMoments[variableName];
- var gradient = variableGradients[variableName];
- tidy(function () {
- var newAccumulatedMeanSquare = _this.decayScalar.mul(accumulatedMeanSquare)
- .add(_this.oneMinusDecay.mul(gradient.square()));
- if (_this.centered) {
- var newAccumulatedMeanGrad = _this.decayScalar.mul(accumulatedMeanGrad)
- .add(_this.oneMinusDecay.mul(gradient));
- var newAccumulatedMoments = _this.momentumScalar.mul(accumulatedMoments)
- .add(_this.c.mul(gradient).div(newAccumulatedMeanSquare
- .sub(newAccumulatedMeanGrad.square().add(_this.epsilonScalar))
- .sqrt()));
- _this.accumulatedMeanSquares[variableName].assign(newAccumulatedMeanSquare);
- _this.accumulatedMeanGrads[variableName].assign(newAccumulatedMeanGrad);
- _this.accumulatedMoments[variableName].assign(newAccumulatedMoments);
- var newValue = value.sub(newAccumulatedMoments);
- value.assign(newValue);
- }
- else {
- var newAccumulatedMeanSquare_1 = _this.decayScalar.mul(accumulatedMeanSquare)
- .add(_this.oneMinusDecay.mul(gradient.square()));
- var newAccumulatedMoments = _this.momentumScalar.mul(accumulatedMoments)
- .add(_this.c.mul(gradient).div(newAccumulatedMeanSquare_1.add(_this.epsilonScalar).sqrt()));
- _this.accumulatedMeanSquares[variableName].assign(newAccumulatedMeanSquare_1);
- _this.accumulatedMoments[variableName].assign(newAccumulatedMoments);
- var newValue = value.sub(newAccumulatedMoments);
- value.assign(newValue);
- }
- });
- };
- var this_1 = this;
- for (var variableName in variableGradients) {
- _loop_1(variableName);
- }
- };
- RMSPropOptimizer.prototype.dispose = function () {
- var _this = this;
- this.c.dispose();
- this.epsilonScalar.dispose();
- this.decayScalar.dispose();
- this.momentumScalar.dispose();
- this.oneMinusDecay.dispose();
- if (this.accumulatedMeanSquares != null) {
- Object.keys(this.accumulatedMeanSquares)
- .forEach(function (name) { return _this.accumulatedMeanSquares[name].dispose(); });
- }
- if (this.accumulatedMeanGrads != null && this.centered) {
- Object.keys(this.accumulatedMeanGrads)
- .forEach(function (name) { return _this.accumulatedMeanGrads[name].dispose(); });
- }
- if (this.accumulatedMoments != null) {
- Object.keys(this.accumulatedMoments)
- .forEach(function (name) { return _this.accumulatedMoments[name].dispose(); });
- }
- };
- RMSPropOptimizer.prototype.getConfig = function () {
- return {
- learningRate: this.learningRate,
- decay: this.decay,
- momentum: this.momentum,
- epsilon: this.epsilon,
- centered: this.centered
- };
- };
- RMSPropOptimizer.fromConfig = function (cls, config) {
- return new cls(config.learningRate, config.decay, config.momentum, config.epsilon, config.centered);
- };
- RMSPropOptimizer.className = 'RMSPropOptimizer';
- return RMSPropOptimizer;
- }(Optimizer));
- SerializationMap.register(RMSPropOptimizer);
-
- var __decorate$t = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var OptimizerConstructors = (function () {
- function OptimizerConstructors() {
- }
- OptimizerConstructors.sgd = function (learningRate) {
- return new SGDOptimizer(learningRate);
- };
- OptimizerConstructors.momentum = function (learningRate, momentum, useNesterov) {
- if (useNesterov === void 0) { useNesterov = false; }
- return new MomentumOptimizer(learningRate, momentum, useNesterov);
- };
- OptimizerConstructors.rmsprop = function (learningRate, decay, momentum, epsilon, centered) {
- if (decay === void 0) { decay = .9; }
- if (momentum === void 0) { momentum = 0.0; }
- if (epsilon === void 0) { epsilon = 1e-8; }
- if (centered === void 0) { centered = false; }
- return new RMSPropOptimizer(learningRate, decay, momentum, epsilon, centered);
- };
- OptimizerConstructors.adam = function (learningRate, beta1, beta2, epsilon) {
- if (learningRate === void 0) { learningRate = 0.001; }
- if (beta1 === void 0) { beta1 = 0.9; }
- if (beta2 === void 0) { beta2 = 0.999; }
- if (epsilon === void 0) { epsilon = 1e-8; }
- return new AdamOptimizer(learningRate, beta1, beta2, epsilon);
- };
- OptimizerConstructors.adadelta = function (learningRate, rho, epsilon) {
- if (learningRate === void 0) { learningRate = .001; }
- if (rho === void 0) { rho = .95; }
- if (epsilon === void 0) { epsilon = 1e-8; }
- return new AdadeltaOptimizer(learningRate, rho, epsilon);
- };
- OptimizerConstructors.adamax = function (learningRate, beta1, beta2, epsilon, decay) {
- if (learningRate === void 0) { learningRate = 0.002; }
- if (beta1 === void 0) { beta1 = 0.9; }
- if (beta2 === void 0) { beta2 = 0.999; }
- if (epsilon === void 0) { epsilon = 1e-8; }
- if (decay === void 0) { decay = 0.0; }
- return new AdamaxOptimizer(learningRate, beta1, beta2, epsilon, decay);
- };
- OptimizerConstructors.adagrad = function (learningRate, initialAccumulatorValue) {
- if (initialAccumulatorValue === void 0) { initialAccumulatorValue = 0.1; }
- return new AdagradOptimizer(learningRate, initialAccumulatorValue);
- };
- __decorate$t([
- doc({ heading: 'Training', subheading: 'Optimizers', namespace: 'train' })
- ], OptimizerConstructors, "sgd", null);
- __decorate$t([
- doc({ heading: 'Training', subheading: 'Optimizers', namespace: 'train' })
- ], OptimizerConstructors, "momentum", null);
- __decorate$t([
- doc({ heading: 'Training', subheading: 'Optimizers', namespace: 'train' })
- ], OptimizerConstructors, "rmsprop", null);
- __decorate$t([
- doc({ heading: 'Training', subheading: 'Optimizers', namespace: 'train' })
- ], OptimizerConstructors, "adam", null);
- __decorate$t([
- doc({ heading: 'Training', subheading: 'Optimizers', namespace: 'train' })
- ], OptimizerConstructors, "adadelta", null);
- __decorate$t([
- doc({ heading: 'Training', subheading: 'Optimizers', namespace: 'train' })
- ], OptimizerConstructors, "adamax", null);
- __decorate$t([
- doc({ heading: 'Training', subheading: 'Optimizers', namespace: 'train' })
- ], OptimizerConstructors, "adagrad", null);
- return OptimizerConstructors;
- }());
-
- var train = {
- sgd: OptimizerConstructors.sgd,
- momentum: OptimizerConstructors.momentum,
- adadelta: OptimizerConstructors.adadelta,
- adagrad: OptimizerConstructors.adagrad,
- rmsprop: OptimizerConstructors.rmsprop,
- adamax: OptimizerConstructors.adamax,
- adam: OptimizerConstructors.adam
- };
-
- var setBackend = Environment.setBackend;
- var getBackend = Environment.getBackend;
- var disposeVariables = Environment.disposeVariables;
- var memory = Environment.memory;
- var nextFrame = BrowserUtil.nextFrame;
-
- var index = /*#__PURE__*/Object.freeze({
- setBackend: setBackend,
- getBackend: getBackend,
- disposeVariables: disposeVariables,
- memory: memory,
- version_core: version,
- nextFrame: nextFrame,
- environment: environment,
- io: io,
- serialization: serialization,
- test_util: test_util,
- util: util,
- webgl: webgl,
- AdadeltaOptimizer: AdadeltaOptimizer,
- AdagradOptimizer: AdagradOptimizer,
- AdamOptimizer: AdamOptimizer,
- AdamaxOptimizer: AdamaxOptimizer,
- MomentumOptimizer: MomentumOptimizer,
- Optimizer: Optimizer,
- RMSPropOptimizer: RMSPropOptimizer,
- SGDOptimizer: SGDOptimizer,
- Tensor: Tensor,
- TensorBuffer: TensorBuffer,
- variable: variable,
- Variable: Variable,
- get Rank () { return Rank; },
- get Reduction () { return Reduction; },
- ENV: ENV,
- Environment: Environment,
- doc: doc,
- batchNormalization: batchNormalization,
- batchNormalization2d: batchNormalization2d,
- batchNormalization3d: batchNormalization3d,
- batchNormalization4d: batchNormalization4d,
- concat: concat,
- concat1d: concat1d,
- concat2d: concat2d,
- concat3d: concat3d,
- concat4d: concat4d,
- conv1d: conv1d,
- conv2d: conv2d,
- conv2dTranspose: conv2dTranspose,
- depthwiseConv2d: depthwiseConv2d,
- separableConv2d: separableConv2d,
- matMul: matMul,
- matrixTimesVector: matrixTimesVector,
- outerProduct: outerProduct,
- vectorTimesMatrix: vectorTimesMatrix,
- avgPool: avgPool,
- maxPool: maxPool,
- transpose: transpose,
- reverse: reverse,
- reverse1d: reverse1d,
- reverse2d: reverse2d,
- reverse3d: reverse3d,
- reverse4d: reverse4d,
- slice: slice,
- slice1d: slice1d,
- slice2d: slice2d,
- slice3d: slice3d,
- slice4d: slice4d,
- stridedSlice: stridedSlice,
- argMax: argMax,
- argMin: argMin,
- logSumExp: logSumExp,
- max: max,
- mean: mean,
- min: min,
- moments: moments,
- sum: sum,
- unsortedSegmentSum: unsortedSegmentSum,
- equal: equal,
- equalStrict: equalStrict,
- greater: greater,
- greaterStrict: greaterStrict,
- greaterEqual: greaterEqual,
- greaterEqualStrict: greaterEqualStrict,
- less: less,
- lessStrict: lessStrict,
- lessEqual: lessEqual,
- lessEqualStrict: lessEqualStrict,
- notEqual: notEqual,
- notEqualStrict: notEqualStrict,
- logicalNot: logicalNot,
- logicalAnd: logicalAnd,
- logicalOr: logicalOr,
- logicalXor: logicalXor,
- where: where,
- abs: abs,
- acos: acos,
- acosh: acosh,
- asin: asin,
- asinh: asinh,
- atan: atan,
- atanh: atanh,
- ceil: ceil,
- clipByValue: clipByValue,
- cos: cos,
- cosh: cosh,
- elu: elu,
- exp: exp,
- expm1: expm1,
- floor: floor,
- sign: sign,
- leakyRelu: leakyRelu,
- log: log,
- log1p: log1p,
- logSigmoid: logSigmoid,
- neg: neg,
- prelu: prelu,
- relu: relu,
- reciprocal: reciprocal,
- round: round,
- selu: selu,
- sigmoid: sigmoid,
- sin: sin,
- sinh: sinh,
- softplus: softplus,
- sqrt: sqrt,
- rsqrt: rsqrt,
- square: square,
- step: step,
- tan: tan,
- tanh: tanh$1,
- erf: erf,
- add: add,
- addStrict: addStrict,
- atan2: atan2,
- div: div,
- divStrict: divStrict,
- maximum: maximum,
- maximumStrict: maximumStrict,
- minimum: minimum,
- minimumStrict: minimumStrict,
- mod: mod,
- modStrict: modStrict,
- mul: mul,
- mulStrict: mulStrict,
- pow: pow,
- powStrict: powStrict,
- sub: sub,
- subStrict: subStrict,
- squaredDifference: squaredDifference,
- squaredDifferenceStrict: squaredDifferenceStrict,
- norm: norm,
- cast: cast,
- clone: clone,
- fromPixels: fromPixels,
- toPixels: toPixels,
- ones: ones,
- onesLike: onesLike,
- zeros: zeros,
- zerosLike: zerosLike,
- eye: eye,
- rand: rand,
- randomNormal: randomNormal,
- truncatedNormal: truncatedNormal,
- randomUniform: randomUniform,
- multinomial: multinomial,
- reshape: reshape,
- squeeze: squeeze,
- tile: tile,
- gather: gather,
- oneHot: oneHot,
- linspace: linspace,
- range: range,
- buffer: buffer,
- fill: fill,
- tensor: tensor,
- scalar: scalar,
- tensor1d: tensor1d,
- tensor2d: tensor2d,
- tensor3d: tensor3d,
- tensor4d: tensor4d,
- print: print,
- expandDims: expandDims,
- stack: stack,
- unstack: unstack,
- split: split,
- cumsum: cumsum,
- pad: pad,
- pad1d: pad1d,
- pad2d: pad2d,
- pad3d: pad3d,
- pad4d: pad4d,
- movingAverage: movingAverage,
- basicLSTMCell: basicLSTMCell,
- multiRNNCell: multiRNNCell,
- softmax: softmax,
- localResponseNormalization: localResponseNormalization,
- linalg: linalg,
- losses: losses,
- image: image,
- operation: operation,
- train: train,
- tidy: tidy,
- keep: keep,
- dispose: dispose,
- time: time,
- grad: grad,
- valueAndGrad: valueAndGrad,
- grads: grads,
- valueAndGrads: valueAndGrads,
- variableGrads: variableGrads,
- customGrad: customGrad
- });
-
- function euclideanDistance(arr1, arr2) {
- if (arr1.length !== arr2.length)
- throw new Error('euclideanDistance: arr1.length !== arr2.length');
- var desc1 = Array.from(arr1);
- var desc2 = Array.from(arr2);
- return Math.sqrt(desc1
- .map(function (val, i) { return val - desc2[i]; })
- .reduce(function (res, diff) { return res + Math.pow(diff, 2); }, 0));
- }
-
- /*! *****************************************************************************
- Copyright (c) Microsoft Corporation. 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
-
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
- MERCHANTABLITY OR NON-INFRINGEMENT.
-
- See the Apache Version 2.0 License for specific language governing permissions
- and limitations under the License.
- ***************************************************************************** */
-
- function __awaiter$e(thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- }
-
- function __generator$e(thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- }
-
- function isFloat(num) {
- return num % 1 !== 0;
- }
- function isEven(num) {
- return num % 2 === 0;
- }
- function round$1(num) {
- return Math.floor(num * 100) / 100;
- }
- function getElement(arg) {
- if (typeof arg === 'string') {
- return document.getElementById(arg);
- }
- return arg;
- }
- function getContext2dOrThrow(canvas) {
- var ctx = canvas.getContext('2d');
- if (!ctx) {
- throw new Error('canvas 2d context is null');
- }
- return ctx;
- }
- function createCanvas(_a) {
- var width = _a.width, height = _a.height;
- var canvas = document.createElement('canvas');
- canvas.width = width;
- canvas.height = height;
- return canvas;
- }
- function createCanvasFromMedia(media, dims) {
- var _a = dims || getMediaDimensions(media), width = _a.width, height = _a.height;
- var canvas = createCanvas({ width: width, height: height });
- getContext2dOrThrow(canvas).drawImage(media, 0, 0, width, height);
- return canvas;
- }
- function getMediaDimensions(media) {
- if (media instanceof HTMLImageElement) {
- return { width: media.naturalWidth, height: media.naturalHeight };
- }
- if (media instanceof HTMLVideoElement) {
- return { width: media.videoWidth, height: media.videoHeight };
- }
- return media;
- }
- function bufferToImage(buf) {
- return new Promise(function (resolve, reject) {
- if (!(buf instanceof Blob)) {
- return reject('bufferToImage - expected buf to be of type: Blob');
- }
- var reader = new FileReader();
- reader.onload = function () {
- var img = new Image();
- img.onload = function () { return resolve(img); };
- img.onerror = reject;
- img.src = reader.result;
- };
- reader.onerror = reject;
- reader.readAsDataURL(buf);
- });
- }
- function imageTensorToCanvas(imgTensor, canvas) {
- return __awaiter$e(this, void 0, void 0, function () {
- var targetCanvas, _a, _, height, width, numChannels;
- return __generator$e(this, function (_b) {
- switch (_b.label) {
- case 0:
- targetCanvas = canvas || document.createElement('canvas');
- _a = imgTensor.shape, _ = _a[0], height = _a[1], width = _a[2], numChannels = _a[3];
- return [4 /*yield*/, toPixels(imgTensor.as3D(height, width, numChannels).toInt(), targetCanvas)];
- case 1:
- _b.sent();
- return [2 /*return*/, targetCanvas];
- }
- });
- });
- }
- function getDefaultDrawOptions() {
- return {
- color: 'blue',
- lineWidth: 2,
- fontSize: 20,
- fontStyle: 'Georgia'
- };
- }
- function drawBox(ctx, x, y, w, h, options) {
- var drawOptions = Object.assign(getDefaultDrawOptions(), (options || {}));
- ctx.strokeStyle = drawOptions.color;
- ctx.lineWidth = drawOptions.lineWidth;
- ctx.strokeRect(x, y, w, h);
- }
- function drawText(ctx, x, y, text, options) {
- var drawOptions = Object.assign(getDefaultDrawOptions(), (options || {}));
- var padText = 2 + drawOptions.lineWidth;
- ctx.fillStyle = drawOptions.color;
- ctx.font = drawOptions.fontSize + "px " + drawOptions.fontStyle;
- ctx.fillText(text, x + padText, y + padText + (drawOptions.fontSize * 0.6));
- }
- function drawDetection(canvasArg, detection, options) {
- var canvas = getElement(canvasArg);
- if (!(canvas instanceof HTMLCanvasElement)) {
- throw new Error('drawBox - expected canvas to be of type: HTMLCanvasElement');
- }
- var detectionArray = Array.isArray(detection)
- ? detection
- : [detection];
- detectionArray.forEach(function (det) {
- var _a = det.getBox(), x = _a.x, y = _a.y, width = _a.width, height = _a.height;
- var drawOptions = Object.assign(getDefaultDrawOptions(), (options || {}));
- var withScore = Object.assign({ withScore: true }, (options || {})).withScore;
- var ctx = getContext2dOrThrow(canvas);
- drawBox(ctx, x, y, width, height, drawOptions);
- if (withScore) {
- drawText(ctx, x, y, "" + round$1(det.getScore()), drawOptions);
- }
- });
- }
- function drawContour(ctx, points, isClosed) {
- if (isClosed === void 0) { isClosed = false; }
- ctx.beginPath();
- points.slice(1).forEach(function (_a, prevIdx) {
- var x = _a.x, y = _a.y;
- var from = points[prevIdx];
- ctx.moveTo(from.x, from.y);
- ctx.lineTo(x, y);
- });
- if (isClosed) {
- var from = points[points.length - 1];
- var to = points[0];
- if (!from || !to) {
- return;
- }
- ctx.moveTo(from.x, from.y);
- ctx.lineTo(to.x, to.y);
- }
- ctx.stroke();
- }
- function drawLandmarks(canvasArg, faceLandmarks, options) {
- var canvas = getElement(canvasArg);
- if (!(canvas instanceof HTMLCanvasElement)) {
- throw new Error('drawLandmarks - expected canvas to be of type: HTMLCanvasElement');
- }
- var drawOptions = Object.assign(getDefaultDrawOptions(), (options || {}));
- var drawLines = Object.assign({ drawLines: false }, (options || {})).drawLines;
- var ctx = getContext2dOrThrow(canvas);
- var lineWidth = drawOptions.lineWidth, color = drawOptions.color;
- if (drawLines) {
- ctx.strokeStyle = color;
- ctx.lineWidth = lineWidth;
- drawContour(ctx, faceLandmarks.getJawOutline());
- drawContour(ctx, faceLandmarks.getLeftEyeBrow());
- drawContour(ctx, faceLandmarks.getRightEyeBrow());
- drawContour(ctx, faceLandmarks.getNose());
- drawContour(ctx, faceLandmarks.getLeftEye(), true);
- drawContour(ctx, faceLandmarks.getRightEye(), true);
- drawContour(ctx, faceLandmarks.getMouth(), true);
- return;
- }
- // else draw points
- var ptOffset = lineWidth / 2;
- ctx.fillStyle = color;
- faceLandmarks.getPositions().forEach(function (pt) { return ctx.fillRect(pt.x - ptOffset, pt.y - ptOffset, lineWidth, lineWidth); });
- }
-
- var NetInput = /** @class */ (function () {
- function NetInput(mediaArg, dims) {
- var _this = this;
- var mediaArgArray = Array.isArray(mediaArg)
- ? mediaArg
- : [mediaArg];
- if (!mediaArgArray.length) {
- throw new Error('NetInput - empty array passed as input');
- }
- var medias = mediaArgArray.map(getElement);
- medias.forEach(function (media, i) {
- if (!(media instanceof HTMLImageElement || media instanceof HTMLVideoElement || media instanceof HTMLCanvasElement)) {
- var idxHint = Array.isArray(mediaArg) ? " at input index " + i + ":" : '';
- if (typeof mediaArgArray[i] === 'string') {
- throw new Error("NetInput -" + idxHint + " string passed, but could not resolve HTMLElement for element id");
- }
- throw new Error("NetInput -" + idxHint + " expected media to be of type HTMLImageElement | HTMLVideoElement | HTMLCanvasElement, or to be an element id");
- }
- });
- this._canvases = [];
- medias.forEach(function (m) { return _this.initCanvas(m, dims); });
- }
- NetInput.prototype.initCanvas = function (media, dims) {
- if (media instanceof HTMLCanvasElement) {
- this._canvases.push(media);
- return;
- }
- // if input is batch type, make sure every canvas has the same dimensions
- var canvasDims = this.dims || dims;
- this._canvases.push(createCanvasFromMedia(media, canvasDims));
- };
- Object.defineProperty(NetInput.prototype, "canvases", {
- get: function () {
- return this._canvases;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(NetInput.prototype, "width", {
- get: function () {
- return (this._canvases[0] || {}).width;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(NetInput.prototype, "height", {
- get: function () {
- return (this._canvases[0] || {}).height;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(NetInput.prototype, "dims", {
- get: function () {
- var _a = this, width = _a.width, height = _a.height;
- return (width > 0 && height > 0) ? { width: width, height: height } : null;
- },
- enumerable: true,
- configurable: true
- });
- return NetInput;
- }());
-
- function getImageTensor(input) {
- return tidy(function () {
- if (input instanceof Tensor) {
- var rank = input.shape.length;
- if (rank !== 3 && rank !== 4) {
- throw new Error('input tensor must be of rank 3 or 4');
- }
- return (rank === 3 ? input.expandDims(0) : input).toFloat();
- }
- var netInput = input instanceof NetInput ? input : new NetInput(input);
- return concat(netInput.canvases.map(function (canvas) {
- return fromPixels(canvas).expandDims(0).toFloat();
- }));
- });
- }
-
- /**
- * Pads the smaller dimension of an image tensor with zeros, such that width === height.
- *
- * @param imgTensor The image tensor.
- * @param isCenterImage (optional, default: false) If true, add padding on both sides of the image, such that the image.
- * @returns The padded tensor with width === height.
- */
- function padToSquare(imgTensor, isCenterImage) {
- if (isCenterImage === void 0) { isCenterImage = false; }
- return tidy(function () {
- var _a = imgTensor.shape.slice(1), height = _a[0], width = _a[1];
- if (height === width) {
- return imgTensor;
- }
- var dimDiff = Math.abs(height - width);
- var paddingAmount = Math.floor(dimDiff * (isCenterImage ? 0.5 : 1));
- var paddingAxis = height > width ? 2 : 1;
- var getPaddingTensorShape = function (isRoundUp) {
- if (isRoundUp === void 0) { isRoundUp = false; }
- var paddingTensorShape = imgTensor.shape.slice();
- paddingTensorShape[paddingAxis] = paddingAmount + (isRoundUp ? 1 : 0);
- return paddingTensorShape;
- };
- var tensorsToStack = (isCenterImage ? [fill(getPaddingTensorShape(!isEven(dimDiff)), 0)] : [])
- .concat([imgTensor, fill(getPaddingTensorShape(), 0)]);
- return concat(tensorsToStack, paddingAxis);
- });
- }
-
- function extractWeightsFactory(weights) {
- var remainingWeights = weights;
- function extractWeights(numWeights) {
- var ret = remainingWeights.slice(0, numWeights);
- remainingWeights = remainingWeights.slice(numWeights);
- return ret;
- }
- function getRemainingWeights() {
- return remainingWeights;
- }
- return {
- extractWeights: extractWeights,
- getRemainingWeights: getRemainingWeights
- };
- }
-
- function extractorsFactory(extractWeights) {
- function extractDepthwiseConvParams(numChannels) {
- var filters = tensor4d(extractWeights(3 * 3 * numChannels), [3, 3, numChannels, 1]);
- var batch_norm_scale = tensor1d(extractWeights(numChannels));
- var batch_norm_offset = tensor1d(extractWeights(numChannels));
- var batch_norm_mean = tensor1d(extractWeights(numChannels));
- var batch_norm_variance = tensor1d(extractWeights(numChannels));
- return {
- filters: filters,
- batch_norm_scale: batch_norm_scale,
- batch_norm_offset: batch_norm_offset,
- batch_norm_mean: batch_norm_mean,
- batch_norm_variance: batch_norm_variance
- };
- }
- function extractConvParams(channelsIn, channelsOut, filterSize) {
- var filters = tensor4d(extractWeights(channelsIn * channelsOut * filterSize * filterSize), [filterSize, filterSize, channelsIn, channelsOut]);
- var bias = tensor1d(extractWeights(channelsOut));
- return {
- filters: filters,
- bias: bias
- };
- }
- function extractPointwiseConvParams(channelsIn, channelsOut, filterSize) {
- var _a = extractConvParams(channelsIn, channelsOut, filterSize), filters = _a.filters, bias = _a.bias;
- return {
- filters: filters,
- batch_norm_offset: bias
- };
- }
- function extractConvPairParams(channelsIn, channelsOut) {
- var depthwise_conv_params = extractDepthwiseConvParams(channelsIn);
- var pointwise_conv_params = extractPointwiseConvParams(channelsIn, channelsOut, 1);
- return {
- depthwise_conv_params: depthwise_conv_params,
- pointwise_conv_params: pointwise_conv_params
- };
- }
- function extractMobilenetV1Params() {
- var conv_0_params = extractPointwiseConvParams(3, 32, 3);
- var channelNumPairs = [
- [32, 64],
- [64, 128],
- [128, 128],
- [128, 256],
- [256, 256],
- [256, 512],
- [512, 512],
- [512, 512],
- [512, 512],
- [512, 512],
- [512, 512],
- [512, 1024],
- [1024, 1024]
- ];
- var conv_pair_params = channelNumPairs.map(function (_a) {
- var channelsIn = _a[0], channelsOut = _a[1];
- return extractConvPairParams(channelsIn, channelsOut);
- });
- return {
- conv_0_params: conv_0_params,
- conv_pair_params: conv_pair_params
- };
- }
- function extractPredictionLayerParams() {
- var conv_0_params = extractPointwiseConvParams(1024, 256, 1);
- var conv_1_params = extractPointwiseConvParams(256, 512, 3);
- var conv_2_params = extractPointwiseConvParams(512, 128, 1);
- var conv_3_params = extractPointwiseConvParams(128, 256, 3);
- var conv_4_params = extractPointwiseConvParams(256, 128, 1);
- var conv_5_params = extractPointwiseConvParams(128, 256, 3);
- var conv_6_params = extractPointwiseConvParams(256, 64, 1);
- var conv_7_params = extractPointwiseConvParams(64, 128, 3);
- var box_encoding_0_predictor_params = extractConvParams(512, 12, 1);
- var class_predictor_0_params = extractConvParams(512, 9, 1);
- var box_encoding_1_predictor_params = extractConvParams(1024, 24, 1);
- var class_predictor_1_params = extractConvParams(1024, 18, 1);
- var box_encoding_2_predictor_params = extractConvParams(512, 24, 1);
- var class_predictor_2_params = extractConvParams(512, 18, 1);
- var box_encoding_3_predictor_params = extractConvParams(256, 24, 1);
- var class_predictor_3_params = extractConvParams(256, 18, 1);
- var box_encoding_4_predictor_params = extractConvParams(256, 24, 1);
- var class_predictor_4_params = extractConvParams(256, 18, 1);
- var box_encoding_5_predictor_params = extractConvParams(128, 24, 1);
- var class_predictor_5_params = extractConvParams(128, 18, 1);
- var box_predictor_0_params = {
- box_encoding_predictor_params: box_encoding_0_predictor_params,
- class_predictor_params: class_predictor_0_params
- };
- var box_predictor_1_params = {
- box_encoding_predictor_params: box_encoding_1_predictor_params,
- class_predictor_params: class_predictor_1_params
- };
- var box_predictor_2_params = {
- box_encoding_predictor_params: box_encoding_2_predictor_params,
- class_predictor_params: class_predictor_2_params
- };
- var box_predictor_3_params = {
- box_encoding_predictor_params: box_encoding_3_predictor_params,
- class_predictor_params: class_predictor_3_params
- };
- var box_predictor_4_params = {
- box_encoding_predictor_params: box_encoding_4_predictor_params,
- class_predictor_params: class_predictor_4_params
- };
- var box_predictor_5_params = {
- box_encoding_predictor_params: box_encoding_5_predictor_params,
- class_predictor_params: class_predictor_5_params
- };
- return {
- conv_0_params: conv_0_params,
- conv_1_params: conv_1_params,
- conv_2_params: conv_2_params,
- conv_3_params: conv_3_params,
- conv_4_params: conv_4_params,
- conv_5_params: conv_5_params,
- conv_6_params: conv_6_params,
- conv_7_params: conv_7_params,
- box_predictor_0_params: box_predictor_0_params,
- box_predictor_1_params: box_predictor_1_params,
- box_predictor_2_params: box_predictor_2_params,
- box_predictor_3_params: box_predictor_3_params,
- box_predictor_4_params: box_predictor_4_params,
- box_predictor_5_params: box_predictor_5_params
- };
- }
- return {
- extractMobilenetV1Params: extractMobilenetV1Params,
- extractPredictionLayerParams: extractPredictionLayerParams
- };
- }
- function extractParams(weights) {
- var _a = extractWeightsFactory(weights), extractWeights = _a.extractWeights, getRemainingWeights = _a.getRemainingWeights;
- var _b = extractorsFactory(extractWeights), extractMobilenetV1Params = _b.extractMobilenetV1Params, extractPredictionLayerParams = _b.extractPredictionLayerParams;
- var mobilenetv1_params = extractMobilenetV1Params();
- var prediction_layer_params = extractPredictionLayerParams();
- var extra_dim = tensor3d(extractWeights(5118 * 4), [1, 5118, 4]);
- var output_layer_params = {
- extra_dim: extra_dim
- };
- if (getRemainingWeights().length !== 0) {
- throw new Error("weights remaing after extract: " + getRemainingWeights().length);
- }
- return {
- mobilenetv1_params: mobilenetv1_params,
- prediction_layer_params: prediction_layer_params,
- output_layer_params: output_layer_params
- };
- }
-
- var Rect = /** @class */ (function () {
- function Rect(x, y, width, height) {
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- }
- Rect.prototype.floor = function () {
- return new Rect(Math.floor(this.x), Math.floor(this.y), Math.floor(this.width), Math.floor(this.height));
- };
- return Rect;
- }());
-
- var FaceDetection = /** @class */ (function () {
- function FaceDetection(score, relativeBox, imageDims) {
- var width = imageDims.width, height = imageDims.height;
- this._imageWidth = width;
- this._imageHeight = height;
- this._score = score;
- this._box = new Rect(relativeBox.x * width, relativeBox.y * height, relativeBox.width * width, relativeBox.height * height);
- }
- FaceDetection.prototype.getScore = function () {
- return this._score;
- };
- FaceDetection.prototype.getBox = function () {
- return this._box;
- };
- FaceDetection.prototype.getImageWidth = function () {
- return this._imageWidth;
- };
- FaceDetection.prototype.getImageHeight = function () {
- return this._imageHeight;
- };
- FaceDetection.prototype.getRelativeBox = function () {
- return new Rect(this._box.x / this._imageWidth, this._box.y / this._imageHeight, this._box.width / this._imageWidth, this._box.height / this._imageHeight);
- };
- FaceDetection.prototype.forSize = function (width, height) {
- return new FaceDetection(this._score, this.getRelativeBox(), { width: width, height: height });
- };
- return FaceDetection;
- }());
-
- function pointwiseConvLayer(x, params, strides) {
- return tidy(function () {
- var out = conv2d(x, params.filters, strides, 'same');
- out = add(out, params.batch_norm_offset);
- return clipByValue(out, 0, 6);
- });
- }
-
- var epsilon = 0.0010000000474974513;
- function depthwiseConvLayer(x, params, strides) {
- return tidy(function () {
- var out = depthwiseConv2d(x, params.filters, strides, 'same');
- out = batchNormalization(out, params.batch_norm_mean, params.batch_norm_variance, epsilon, params.batch_norm_scale, params.batch_norm_offset);
- return clipByValue(out, 0, 6);
- });
- }
- function getStridesForLayerIdx(layerIdx) {
- return [2, 4, 6, 12].some(function (idx) { return idx === layerIdx; }) ? [2, 2] : [1, 1];
- }
- function mobileNetV1(x, params) {
- return tidy(function () {
- var conv11 = null;
- var out = pointwiseConvLayer(x, params.conv_0_params, [2, 2]);
- params.conv_pair_params.forEach(function (param, i) {
- var layerIdx = i + 1;
- var depthwiseConvStrides = getStridesForLayerIdx(layerIdx);
- out = depthwiseConvLayer(out, param.depthwise_conv_params, depthwiseConvStrides);
- out = pointwiseConvLayer(out, param.pointwise_conv_params, [1, 1]);
- if (layerIdx === 11) {
- conv11 = out;
- }
- });
- if (conv11 === null) {
- throw new Error('mobileNetV1 - output of conv layer 11 is null');
- }
- return {
- out: out,
- conv11: conv11
- };
- });
- }
-
- function nonMaxSuppression(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold) {
- var numBoxes = boxes.shape[0];
- var outputSize = Math.min(maxOutputSize, numBoxes);
- var candidates = scores
- .map(function (score, boxIndex) { return ({ score: score, boxIndex: boxIndex }); })
- .filter(function (c) { return c.score > scoreThreshold; })
- .sort(function (c1, c2) { return c2.score - c1.score; });
- var suppressFunc = function (x) { return x <= iouThreshold ? 1 : 0; };
- var selected = [];
- candidates.forEach(function (c) {
- if (selected.length >= outputSize) {
- return;
- }
- var originalScore = c.score;
- for (var j = selected.length - 1; j >= 0; --j) {
- var iou = IOU(boxes, c.boxIndex, selected[j]);
- if (iou === 0.0) {
- continue;
- }
- c.score *= suppressFunc(iou);
- if (c.score <= scoreThreshold) {
- break;
- }
- }
- if (originalScore === c.score) {
- selected.push(c.boxIndex);
- }
- });
- return selected;
- }
- function IOU(boxes, i, j) {
- var yminI = Math.min(boxes.get(i, 0), boxes.get(i, 2));
- var xminI = Math.min(boxes.get(i, 1), boxes.get(i, 3));
- var ymaxI = Math.max(boxes.get(i, 0), boxes.get(i, 2));
- var xmaxI = Math.max(boxes.get(i, 1), boxes.get(i, 3));
- var yminJ = Math.min(boxes.get(j, 0), boxes.get(j, 2));
- var xminJ = Math.min(boxes.get(j, 1), boxes.get(j, 3));
- var ymaxJ = Math.max(boxes.get(j, 0), boxes.get(j, 2));
- var xmaxJ = Math.max(boxes.get(j, 1), boxes.get(j, 3));
- var areaI = (ymaxI - yminI) * (xmaxI - xminI);
- var areaJ = (ymaxJ - yminJ) * (xmaxJ - xminJ);
- if (areaI <= 0 || areaJ <= 0) {
- return 0.0;
- }
- var intersectionYmin = Math.max(yminI, yminJ);
- var intersectionXmin = Math.max(xminI, xminJ);
- var intersectionYmax = Math.min(ymaxI, ymaxJ);
- var intersectionXmax = Math.min(xmaxI, xmaxJ);
- var intersectionArea = Math.max(intersectionYmax - intersectionYmin, 0.0) *
- Math.max(intersectionXmax - intersectionXmin, 0.0);
- return intersectionArea / (areaI + areaJ - intersectionArea);
- }
-
- function getCenterCoordinatesAndSizesLayer(x) {
- var vec = unstack(transpose(x, [1, 0]));
- var sizes = [
- sub(vec[2], vec[0]),
- sub(vec[3], vec[1])
- ];
- var centers = [
- add(vec[0], div(sizes[0], scalar(2))),
- add(vec[1], div(sizes[1], scalar(2)))
- ];
- return {
- sizes: sizes,
- centers: centers
- };
- }
- function decodeBoxesLayer(x0, x1) {
- var _a = getCenterCoordinatesAndSizesLayer(x0), sizes = _a.sizes, centers = _a.centers;
- var vec = unstack(transpose(x1, [1, 0]));
- var div0_out = div(mul(exp(div(vec[2], scalar(5))), sizes[0]), scalar(2));
- var add0_out = add(mul(div(vec[0], scalar(10)), sizes[0]), centers[0]);
- var div1_out = div(mul(exp(div(vec[3], scalar(5))), sizes[1]), scalar(2));
- var add1_out = add(mul(div(vec[1], scalar(10)), sizes[1]), centers[1]);
- return transpose(stack([
- sub(add0_out, div0_out),
- sub(add1_out, div1_out),
- add(add0_out, div0_out),
- add(add1_out, div1_out)
- ]), [1, 0]);
- }
- function outputLayer(boxPredictions, classPredictions, params) {
- return tidy(function () {
- var batchSize = boxPredictions.shape[0];
- var boxes = decodeBoxesLayer(reshape(tile(params.extra_dim, [batchSize, 1, 1]), [-1, 4]), reshape(boxPredictions, [-1, 4]));
- boxes = reshape(boxes, [batchSize, (boxes.shape[0] / batchSize), 4]);
- var scoresAndClasses = sigmoid(slice(classPredictions, [0, 0, 1], [-1, -1, -1]));
- var scores = slice(scoresAndClasses, [0, 0, 0], [-1, -1, 1]);
- scores = reshape(scores, [batchSize, scores.shape[1]]);
- var boxesByBatch = unstack(boxes);
- var scoresByBatch = unstack(scores);
- return {
- boxes: boxesByBatch,
- scores: scoresByBatch
- };
- });
- }
-
- function convLayer(x, params, padding, withRelu) {
- if (padding === void 0) { padding = 'same'; }
- if (withRelu === void 0) { withRelu = false; }
- return tidy(function () {
- var out = add(conv2d(x, params.filters, [1, 1], padding), params.bias);
- return withRelu ? relu(out) : out;
- });
- }
-
- function boxPredictionLayer(x, params) {
- return tidy(function () {
- var batchSize = x.shape[0];
- var boxPredictionEncoding = reshape(convLayer(x, params.box_encoding_predictor_params), [batchSize, -1, 1, 4]);
- var classPrediction = reshape(convLayer(x, params.class_predictor_params), [batchSize, -1, 3]);
- return {
- boxPredictionEncoding: boxPredictionEncoding,
- classPrediction: classPrediction
- };
- });
- }
-
- function predictionLayer(x, conv11, params) {
- return tidy(function () {
- var conv0 = pointwiseConvLayer(x, params.conv_0_params, [1, 1]);
- var conv1 = pointwiseConvLayer(conv0, params.conv_1_params, [2, 2]);
- var conv2 = pointwiseConvLayer(conv1, params.conv_2_params, [1, 1]);
- var conv3 = pointwiseConvLayer(conv2, params.conv_3_params, [2, 2]);
- var conv4 = pointwiseConvLayer(conv3, params.conv_4_params, [1, 1]);
- var conv5 = pointwiseConvLayer(conv4, params.conv_5_params, [2, 2]);
- var conv6 = pointwiseConvLayer(conv5, params.conv_6_params, [1, 1]);
- var conv7 = pointwiseConvLayer(conv6, params.conv_7_params, [2, 2]);
- var boxPrediction0 = boxPredictionLayer(conv11, params.box_predictor_0_params);
- var boxPrediction1 = boxPredictionLayer(x, params.box_predictor_1_params);
- var boxPrediction2 = boxPredictionLayer(conv1, params.box_predictor_2_params);
- var boxPrediction3 = boxPredictionLayer(conv3, params.box_predictor_3_params);
- var boxPrediction4 = boxPredictionLayer(conv5, params.box_predictor_4_params);
- var boxPrediction5 = boxPredictionLayer(conv7, params.box_predictor_5_params);
- var boxPredictions = concat([
- boxPrediction0.boxPredictionEncoding,
- boxPrediction1.boxPredictionEncoding,
- boxPrediction2.boxPredictionEncoding,
- boxPrediction3.boxPredictionEncoding,
- boxPrediction4.boxPredictionEncoding,
- boxPrediction5.boxPredictionEncoding
- ], 1);
- var classPredictions = concat([
- boxPrediction0.classPrediction,
- boxPrediction1.classPrediction,
- boxPrediction2.classPrediction,
- boxPrediction3.classPrediction,
- boxPrediction4.classPrediction,
- boxPrediction5.classPrediction
- ], 1);
- return {
- boxPredictions: boxPredictions,
- classPredictions: classPredictions
- };
- });
- }
-
- var resizedImageSize = [512, 512];
- var weight = scalar(0.007843137718737125);
- var bias = scalar(1);
- function resizeLayer(x) {
- return tidy(function () {
- var resized = image.resizeBilinear(x, resizedImageSize, false);
- return sub(mul(resized, weight), bias);
- });
- }
-
- function faceDetectionNet(weights) {
- var params = extractParams(weights);
- function forwardTensor(imgTensor) {
- return tidy(function () {
- var resized = resizeLayer(imgTensor);
- var features = mobileNetV1(resized, params.mobilenetv1_params);
- var _a = predictionLayer(features.out, features.conv11, params.prediction_layer_params), boxPredictions = _a.boxPredictions, classPredictions = _a.classPredictions;
- return outputLayer(boxPredictions, classPredictions, params.output_layer_params);
- });
- }
- function forward(input) {
- return tidy(function () { return forwardTensor(padToSquare(getImageTensor(input))); });
- }
- function locateFaces(input, minConfidence, maxResults) {
- if (minConfidence === void 0) { minConfidence = 0.8; }
- if (maxResults === void 0) { maxResults = 100; }
- return __awaiter$e(this, void 0, void 0, function () {
- var paddedHeightRelative, paddedWidthRelative, imageDimensions, _a, _boxes, _scores, boxes, scores, i, scoresData, _b, _c, iouThreshold, indices, results;
- return __generator$e(this, function (_d) {
- switch (_d.label) {
- case 0:
- paddedHeightRelative = 1, paddedWidthRelative = 1;
- _a = tidy(function () {
- var imgTensor = getImageTensor(input);
- var _a = imgTensor.shape.slice(1), height = _a[0], width = _a[1];
- imageDimensions = { width: width, height: height };
- imgTensor = padToSquare(imgTensor);
- paddedHeightRelative = imgTensor.shape[1] / height;
- paddedWidthRelative = imgTensor.shape[2] / width;
- return forwardTensor(imgTensor);
- }), _boxes = _a.boxes, _scores = _a.scores;
- boxes = _boxes[0];
- scores = _scores[0];
- for (i = 1; i < _boxes.length; i++) {
- _boxes[i].dispose();
- _scores[i].dispose();
- }
- _c = (_b = Array).from;
- return [4 /*yield*/, scores.data()];
- case 1:
- scoresData = _c.apply(_b, [_d.sent()]);
- iouThreshold = 0.5;
- indices = nonMaxSuppression(boxes, scoresData, maxResults, iouThreshold, minConfidence);
- results = indices
- .map(function (idx) {
- var _a = [
- Math.max(0, boxes.get(idx, 0)),
- Math.min(1.0, boxes.get(idx, 2))
- ].map(function (val) { return val * paddedHeightRelative; }), top = _a[0], bottom = _a[1];
- var _b = [
- Math.max(0, boxes.get(idx, 1)),
- Math.min(1.0, boxes.get(idx, 3))
- ].map(function (val) { return val * paddedWidthRelative; }), left = _b[0], right = _b[1];
- return new FaceDetection(scoresData[idx], new Rect(left, top, right - left, bottom - top), imageDimensions);
- });
- boxes.dispose();
- scores.dispose();
- return [2 /*return*/, results];
- }
- });
- });
- }
- return {
- forward: forward,
- locateFaces: locateFaces
- };
- }
-
- var Point = /** @class */ (function () {
- function Point(x, y) {
- this.x = x;
- this.y = y;
- }
- Point.prototype.add = function (pt) {
- return new Point(this.x + pt.x, this.y + pt.y);
- };
- Point.prototype.sub = function (pt) {
- return new Point(this.x - pt.x, this.y - pt.y);
- };
- Point.prototype.mul = function (pt) {
- return new Point(this.x * pt.x, this.y * pt.y);
- };
- Point.prototype.div = function (pt) {
- return new Point(this.x / pt.x, this.y / pt.y);
- };
- Point.prototype.abs = function () {
- return new Point(Math.abs(this.x), Math.abs(this.y));
- };
- Point.prototype.magnitude = function () {
- return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
- };
- Point.prototype.floor = function () {
- return new Point(Math.floor(this.x), Math.floor(this.y));
- };
- return Point;
- }());
-
- function extractConvParamsFactory(extractWeights) {
- return function (channelsIn, channelsOut, filterSize) {
- var filters = tensor4d(extractWeights(channelsIn * channelsOut * filterSize * filterSize), [filterSize, filterSize, channelsIn, channelsOut]);
- var bias = tensor1d(extractWeights(channelsOut));
- return {
- filters: filters,
- bias: bias
- };
- };
- }
-
- function extractParams$1(weights) {
- var _a = extractWeightsFactory(weights), extractWeights = _a.extractWeights, getRemainingWeights = _a.getRemainingWeights;
- var extractConvParams = extractConvParamsFactory(extractWeights);
- function extractFcParams(channelsIn, channelsOut) {
- var fc_weights = tensor2d(extractWeights(channelsIn * channelsOut), [channelsIn, channelsOut]);
- var fc_bias = tensor1d(extractWeights(channelsOut));
- return {
- weights: fc_weights,
- bias: fc_bias
- };
- }
- var conv0_params = extractConvParams(3, 32, 3);
- var conv1_params = extractConvParams(32, 64, 3);
- var conv2_params = extractConvParams(64, 64, 3);
- var conv3_params = extractConvParams(64, 64, 3);
- var conv4_params = extractConvParams(64, 64, 3);
- var conv5_params = extractConvParams(64, 128, 3);
- var conv6_params = extractConvParams(128, 128, 3);
- var conv7_params = extractConvParams(128, 256, 3);
- var fc0_params = extractFcParams(6400, 1024);
- var fc1_params = extractFcParams(1024, 136);
- if (getRemainingWeights().length !== 0) {
- throw new Error("weights remaing after extract: " + getRemainingWeights().length);
- }
- return {
- conv0_params: conv0_params,
- conv1_params: conv1_params,
- conv2_params: conv2_params,
- conv3_params: conv3_params,
- conv4_params: conv4_params,
- conv5_params: conv5_params,
- conv6_params: conv6_params,
- conv7_params: conv7_params,
- fc0_params: fc0_params,
- fc1_params: fc1_params
- };
- }
-
- function getCenterPoint(pts) {
- return pts.reduce(function (sum, pt) { return sum.add(pt); }, new Point(0, 0))
- .div(new Point(pts.length, pts.length));
- }
-
- // face alignment constants
- var relX = 0.5;
- var relY = 0.43;
- var relScale = 0.45;
- var FaceLandmarks = /** @class */ (function () {
- function FaceLandmarks(relativeFaceLandmarkPositions, imageDims, shift) {
- if (shift === void 0) { shift = new Point(0, 0); }
- var width = imageDims.width, height = imageDims.height;
- this._imageWidth = width;
- this._imageHeight = height;
- this._shift = shift;
- this._faceLandmarks = relativeFaceLandmarkPositions.map(function (pt) { return pt.mul(new Point(width, height)).add(shift); });
- }
- FaceLandmarks.prototype.getShift = function () {
- return new Point(this._shift.x, this._shift.y);
- };
- FaceLandmarks.prototype.getImageWidth = function () {
- return this._imageWidth;
- };
- FaceLandmarks.prototype.getImageHeight = function () {
- return this._imageHeight;
- };
- FaceLandmarks.prototype.getPositions = function () {
- return this._faceLandmarks;
- };
- FaceLandmarks.prototype.getRelativePositions = function () {
- var _this = this;
- return this._faceLandmarks.map(function (pt) { return pt.sub(_this._shift).div(new Point(_this._imageWidth, _this._imageHeight)); });
- };
- FaceLandmarks.prototype.getJawOutline = function () {
- return this._faceLandmarks.slice(0, 17);
- };
- FaceLandmarks.prototype.getLeftEyeBrow = function () {
- return this._faceLandmarks.slice(17, 22);
- };
- FaceLandmarks.prototype.getRightEyeBrow = function () {
- return this._faceLandmarks.slice(22, 27);
- };
- FaceLandmarks.prototype.getNose = function () {
- return this._faceLandmarks.slice(27, 36);
- };
- FaceLandmarks.prototype.getLeftEye = function () {
- return this._faceLandmarks.slice(36, 42);
- };
- FaceLandmarks.prototype.getRightEye = function () {
- return this._faceLandmarks.slice(42, 48);
- };
- FaceLandmarks.prototype.getMouth = function () {
- return this._faceLandmarks.slice(48, 68);
- };
- FaceLandmarks.prototype.forSize = function (width, height) {
- return new FaceLandmarks(this.getRelativePositions(), { width: width, height: height });
- };
- FaceLandmarks.prototype.shift = function (x, y) {
- return new FaceLandmarks(this.getRelativePositions(), { width: this._imageWidth, height: this._imageHeight }, new Point(x, y));
- };
- /**
- * Aligns the face landmarks after face detection from the relative positions of the faces
- * bounding box, or it's current shift. This function should be used to align the face images
- * after face detection has been performed, before they are passed to the face recognition net.
- * This will make the computed face descriptor more accurate.
- *
- * @param detection (optional) The bounding box of the face or the face detection result. If
- * no argument was passed the position of the face landmarks are assumed to be relative to
- * it's current shift.
- * @returns The bounding box of the aligned face.
- */
- FaceLandmarks.prototype.align = function (detection) {
- if (detection) {
- var box = detection instanceof FaceDetection
- ? detection.getBox().floor()
- : detection;
- return this.shift(box.x, box.y).align();
- }
- var centers = [
- this.getLeftEye(),
- this.getRightEye(),
- this.getMouth()
- ].map(getCenterPoint);
- var leftEyeCenter = centers[0], rightEyeCenter = centers[1], mouthCenter = centers[2];
- var distToMouth = function (pt) { return mouthCenter.sub(pt).magnitude(); };
- var eyeToMouthDist = (distToMouth(leftEyeCenter) + distToMouth(rightEyeCenter)) / 2;
- var size = Math.floor(eyeToMouthDist / relScale);
- var refPoint = getCenterPoint(centers);
- // TODO: pad in case rectangle is out of image bounds
- var x = Math.floor(Math.max(0, refPoint.x - (relX * size)));
- var y = Math.floor(Math.max(0, refPoint.y - (relY * size)));
- return new Rect(x, y, size, size);
- };
- return FaceLandmarks;
- }());
-
- function fullyConnectedLayer(x, params) {
- return tidy(function () {
- return add(matMul(x, params.weights), params.bias);
- });
- }
-
- function conv(x, params) {
- return convLayer(x, params, 'valid', true);
- }
- function maxPool$1(x, strides) {
- if (strides === void 0) { strides = [2, 2]; }
- return maxPool(x, [2, 2], strides, 'valid');
- }
- function faceLandmarkNet(weights) {
- var params = extractParams$1(weights);
- function detectLandmarks(input) {
- return __awaiter$e(this, void 0, void 0, function () {
- var imageDimensions, outTensor, faceLandmarksArray, _a, _b, xCoords, yCoords;
- return __generator$e(this, function (_c) {
- switch (_c.label) {
- case 0:
- outTensor = tidy(function () {
- var imgTensor = getImageTensor(input);
- var _a = imgTensor.shape.slice(1), height = _a[0], width = _a[1];
- imageDimensions = { width: width, height: height };
- // work with 128 x 128 sized face images
- if (imgTensor.shape[1] !== 128 || imgTensor.shape[2] !== 128) {
- imgTensor = image.resizeBilinear(imgTensor, [128, 128]);
- }
- var out = conv(imgTensor, params.conv0_params);
- out = maxPool$1(out);
- out = conv(out, params.conv1_params);
- out = conv(out, params.conv2_params);
- out = maxPool$1(out);
- out = conv(out, params.conv3_params);
- out = conv(out, params.conv4_params);
- out = maxPool$1(out);
- out = conv(out, params.conv5_params);
- out = conv(out, params.conv6_params);
- out = maxPool$1(out, [1, 1]);
- out = conv(out, params.conv7_params);
- var fc0 = relu(fullyConnectedLayer(out.as2D(out.shape[0], -1), params.fc0_params));
- var fc1 = fullyConnectedLayer(fc0, params.fc1_params);
- return fc1;
- });
- _b = (_a = Array).from;
- return [4 /*yield*/, outTensor.data()];
- case 1:
- faceLandmarksArray = _b.apply(_a, [_c.sent()]);
- outTensor.dispose();
- xCoords = faceLandmarksArray.filter(function (c, i) { return (i - 1) % 2; });
- yCoords = faceLandmarksArray.filter(function (c, i) { return i % 2; });
- return [2 /*return*/, new FaceLandmarks(Array(68).fill(0).map(function (_, i) { return new Point(xCoords[i], yCoords[i]); }), imageDimensions)];
- }
- });
- });
- }
- return {
- detectLandmarks: detectLandmarks
- };
- }
-
- function scale(x, params) {
- return add(mul(x, params.weights), params.biases);
- }
-
- function convLayer$1(x, params, strides, withRelu, padding) {
- if (padding === void 0) { padding = 'same'; }
- var _a = params.conv, filters = _a.filters, bias = _a.bias;
- var out = conv2d(x, filters, strides, padding);
- out = add(out, bias);
- out = scale(out, params.scale);
- return withRelu ? relu(out) : out;
- }
- function conv$1(x, params) {
- return convLayer$1(x, params, [1, 1], true);
- }
- function convNoRelu(x, params) {
- return convLayer$1(x, params, [1, 1], false);
- }
- function convDown(x, params) {
- return convLayer$1(x, params, [2, 2], true, 'valid');
- }
-
- function extractorsFactory$1(extractWeights) {
- function extractFilterValues(numFilterValues, numFilters, filterSize) {
- var weights = extractWeights(numFilterValues);
- var depth = weights.length / (numFilters * filterSize * filterSize);
- if (isFloat(depth)) {
- throw new Error("depth has to be an integer: " + depth + ", weights.length: " + weights.length + ", numFilters: " + numFilters + ", filterSize: " + filterSize);
- }
- return transpose(tensor4d(weights, [numFilters, depth, filterSize, filterSize]), [2, 3, 1, 0]);
- }
- function extractScaleLayerParams(numWeights) {
- var weights = tensor1d(extractWeights(numWeights));
- var biases = tensor1d(extractWeights(numWeights));
- return {
- weights: weights,
- biases: biases
- };
- }
- function extractConvLayerParams(numFilterValues, numFilters, filterSize) {
- var conv_filters = extractFilterValues(numFilterValues, numFilters, filterSize);
- var conv_bias = tensor1d(extractWeights(numFilters));
- var scale = extractScaleLayerParams(numFilters);
- return {
- conv: {
- filters: conv_filters,
- bias: conv_bias
- },
- scale: scale
- };
- }
- function extractResidualLayerParams(numFilterValues, numFilters, filterSize, isDown) {
- if (isDown === void 0) { isDown = false; }
- var conv1 = extractConvLayerParams((isDown ? 0.5 : 1) * numFilterValues, numFilters, filterSize);
- var conv2 = extractConvLayerParams(numFilterValues, numFilters, filterSize);
- return {
- conv1: conv1,
- conv2: conv2
- };
- }
- return {
- extractConvLayerParams: extractConvLayerParams,
- extractResidualLayerParams: extractResidualLayerParams
- };
- }
- function extractParams$2(weights) {
- var _a = extractWeightsFactory(weights), extractWeights = _a.extractWeights, getRemainingWeights = _a.getRemainingWeights;
- var _b = extractorsFactory$1(extractWeights), extractConvLayerParams = _b.extractConvLayerParams, extractResidualLayerParams = _b.extractResidualLayerParams;
- var conv32_down = extractConvLayerParams(4704, 32, 7);
- var conv32_1 = extractResidualLayerParams(9216, 32, 3);
- var conv32_2 = extractResidualLayerParams(9216, 32, 3);
- var conv32_3 = extractResidualLayerParams(9216, 32, 3);
- var conv64_down = extractResidualLayerParams(36864, 64, 3, true);
- var conv64_1 = extractResidualLayerParams(36864, 64, 3);
- var conv64_2 = extractResidualLayerParams(36864, 64, 3);
- var conv64_3 = extractResidualLayerParams(36864, 64, 3);
- var conv128_down = extractResidualLayerParams(147456, 128, 3, true);
- var conv128_1 = extractResidualLayerParams(147456, 128, 3);
- var conv128_2 = extractResidualLayerParams(147456, 128, 3);
- var conv256_down = extractResidualLayerParams(589824, 256, 3, true);
- var conv256_1 = extractResidualLayerParams(589824, 256, 3);
- var conv256_2 = extractResidualLayerParams(589824, 256, 3);
- var conv256_down_out = extractResidualLayerParams(589824, 256, 3);
- var fc = transpose(tensor2d(extractWeights(256 * 128), [128, 256]), [1, 0]);
- if (getRemainingWeights().length !== 0) {
- throw new Error("weights remaing after extract: " + getRemainingWeights().length);
- }
- return {
- conv32_down: conv32_down,
- conv32_1: conv32_1,
- conv32_2: conv32_2,
- conv32_3: conv32_3,
- conv64_down: conv64_down,
- conv64_1: conv64_1,
- conv64_2: conv64_2,
- conv64_3: conv64_3,
- conv128_down: conv128_down,
- conv128_1: conv128_1,
- conv128_2: conv128_2,
- conv256_down: conv256_down,
- conv256_1: conv256_1,
- conv256_2: conv256_2,
- conv256_down_out: conv256_down_out,
- fc: fc
- };
- }
-
- function normalize(x) {
- return tidy(function () {
- var avg_r = fill([1, 150, 150, 1], 122.782);
- var avg_g = fill([1, 150, 150, 1], 117.001);
- var avg_b = fill([1, 150, 150, 1], 104.298);
- var avg_rgb = concat([avg_r, avg_g, avg_b], 3);
- return div(sub(x, avg_rgb), scalar(256));
- });
- }
-
- function residual(x, params) {
- var out = conv$1(x, params.conv1);
- out = convNoRelu(out, params.conv2);
- out = add(out, x);
- out = relu(out);
- return out;
- }
- function residualDown(x, params) {
- var out = convDown(x, params.conv1);
- out = convNoRelu(out, params.conv2);
- var pooled = avgPool(x, 2, 2, 'valid');
- var zeros$$1 = zeros(pooled.shape);
- var isPad = pooled.shape[3] !== out.shape[3];
- var isAdjustShape = pooled.shape[1] !== out.shape[1] || pooled.shape[2] !== out.shape[2];
- if (isAdjustShape) {
- var padShapeX = out.shape.slice();
- padShapeX[1] = 1;
- var zerosW = zeros(padShapeX);
- out = concat([out, zerosW], 1);
- var padShapeY = out.shape.slice();
- padShapeY[2] = 1;
- var zerosH = zeros(padShapeY);
- out = concat([out, zerosH], 2);
- }
- pooled = isPad ? concat([pooled, zeros$$1], 3) : pooled;
- out = add(pooled, out);
- out = relu(out);
- return out;
- }
-
- function faceRecognitionNet(weights) {
- var _this = this;
- var params = extractParams$2(weights);
- function forward(input) {
- return tidy(function () {
- var x = padToSquare(getImageTensor(input), true);
- // work with 150 x 150 sized face images
- if (x.shape[1] !== 150 || x.shape[2] !== 150) {
- x = image.resizeBilinear(x, [150, 150]);
- }
- x = normalize(x);
- var out = convDown(x, params.conv32_down);
- out = maxPool(out, 3, 2, 'valid');
- out = residual(out, params.conv32_1);
- out = residual(out, params.conv32_2);
- out = residual(out, params.conv32_3);
- out = residualDown(out, params.conv64_down);
- out = residual(out, params.conv64_1);
- out = residual(out, params.conv64_2);
- out = residual(out, params.conv64_3);
- out = residualDown(out, params.conv128_down);
- out = residual(out, params.conv128_1);
- out = residual(out, params.conv128_2);
- out = residualDown(out, params.conv256_down);
- out = residual(out, params.conv256_1);
- out = residual(out, params.conv256_2);
- out = residualDown(out, params.conv256_down_out);
- var globalAvg = out.mean([1, 2]);
- var fullyConnected = matMul(globalAvg, params.fc);
- return fullyConnected;
- });
- }
- var computeFaceDescriptor = function (input) { return __awaiter$e(_this, void 0, void 0, function () {
- var result, data;
- return __generator$e(this, function (_a) {
- switch (_a.label) {
- case 0:
- result = forward(input);
- return [4 /*yield*/, result.data()];
- case 1:
- data = _a.sent();
- result.dispose();
- return [2 /*return*/, data];
- }
- });
- }); };
- var computeFaceDescriptorSync = function (input) {
- var result = forward(input);
- var data = result.dataSync();
- result.dispose();
- return data;
- };
- return {
- computeFaceDescriptor: computeFaceDescriptor,
- computeFaceDescriptorSync: computeFaceDescriptorSync,
- forward: forward
- };
- }
-
- /**
- * Extracts the image regions containing the detected faces.
- *
- * @param input The image that face detection has been performed on.
- * @param detections The face detection results or face bounding boxes for that image.
- * @returns The Canvases of the corresponding image region for each detected face.
- */
- function extractFaces(image, detections) {
- var ctx = getContext2dOrThrow(image);
- var boxes = detections.map(function (det) { return det instanceof FaceDetection
- ? det.forSize(image.width, image.height).getBox().floor()
- : det; });
- return boxes.map(function (_a) {
- var x = _a.x, y = _a.y, width = _a.width, height = _a.height;
- var faceImg = createCanvas({ width: width, height: height });
- getContext2dOrThrow(faceImg)
- .putImageData(ctx.getImageData(x, y, width, height), 0, 0);
- return faceImg;
- });
- }
-
- /**
- * Extracts the tensors of the image regions containing the detected faces.
- * Useful if you want to compute the face descriptors for the face images.
- * Using this method is faster then extracting a canvas for each face and
- * converting them to tensors individually.
- *
- * @param input The image that face detection has been performed on.
- * @param detections The face detection results or face bounding boxes for that image.
- * @returns Tensors of the corresponding image region for each detected face.
- */
- function extractFaceTensors(image$$1, detections) {
- return tidy(function () {
- var imgTensor = getImageTensor(image$$1);
- // TODO handle batches
- var _a = imgTensor.shape, batchSize = _a[0], imgHeight = _a[1], imgWidth = _a[2], numChannels = _a[3];
- var boxes = detections.map(function (det) { return det instanceof FaceDetection
- ? det.forSize(imgWidth, imgHeight).getBox().floor()
- : det; });
- var faceTensors = boxes.map(function (_a) {
- var x = _a.x, y = _a.y, width = _a.width, height = _a.height;
- return slice(imgTensor, [0, y, x, 0], [1, height, width, numChannels]);
- });
- return faceTensors;
- });
- }
-
- exports.euclideanDistance = euclideanDistance;
- exports.faceDetectionNet = faceDetectionNet;
- exports.faceLandmarkNet = faceLandmarkNet;
- exports.faceRecognitionNet = faceRecognitionNet;
- exports.NetInput = NetInput;
- exports.tf = index;
- exports.padToSquare = padToSquare;
- exports.extractFaces = extractFaces;
- exports.extractFaceTensors = extractFaceTensors;
- exports.isFloat = isFloat;
- exports.isEven = isEven;
- exports.round = round$1;
- exports.getElement = getElement;
- exports.getContext2dOrThrow = getContext2dOrThrow;
- exports.createCanvas = createCanvas;
- exports.createCanvasFromMedia = createCanvasFromMedia;
- exports.getMediaDimensions = getMediaDimensions;
- exports.bufferToImage = bufferToImage;
- exports.imageTensorToCanvas = imageTensorToCanvas;
- exports.getDefaultDrawOptions = getDefaultDrawOptions;
- exports.drawBox = drawBox;
- exports.drawText = drawText;
- exports.drawDetection = drawDetection;
- exports.drawLandmarks = drawLandmarks;
-
- Object.defineProperty(exports, '__esModule', { value: true });
+ FaceRecognitionNet.prototype.load = function (weightsOrUrl) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var _a;
+ return __generator$1(this, function (_b) {
+ switch (_b.label) {
+ case 0:
+ if (weightsOrUrl instanceof Float32Array) {
+ this.extractWeights(weightsOrUrl);
+ return [2 /*return*/];
+ }
+ if (weightsOrUrl && typeof weightsOrUrl !== 'string') {
+ throw new Error('FaceLandmarkNet.load - expected model uri, or weights as Float32Array');
+ }
+ _a = this;
+ return [4 /*yield*/, loadQuantizedParams$2(weightsOrUrl)];
+ case 1:
+ _a._params = _b.sent();
+ return [2 /*return*/];
+ }
+ });
+ });
+ };
+ FaceRecognitionNet.prototype.extractWeights = function (weights) {
+ this._params = extractParams$2(weights);
+ };
+ FaceRecognitionNet.prototype.forward = function (input) {
+ var _this = this;
+ if (!this._params) {
+ throw new Error('FaceRecognitionNet - load model before inference');
+ }
+ return tidy(function () {
+ var x = padToSquare(getImageTensor(input), true);
+ // work with 150 x 150 sized face images
+ if (x.shape[1] !== 150 || x.shape[2] !== 150) {
+ x = image.resizeBilinear(x, [150, 150]);
+ }
+ x = normalize(x);
+ var out = convDown(x, _this._params.conv32_down);
+ out = maxPool(out, 3, 2, 'valid');
+ out = residual(out, _this._params.conv32_1);
+ out = residual(out, _this._params.conv32_2);
+ out = residual(out, _this._params.conv32_3);
+ out = residualDown(out, _this._params.conv64_down);
+ out = residual(out, _this._params.conv64_1);
+ out = residual(out, _this._params.conv64_2);
+ out = residual(out, _this._params.conv64_3);
+ out = residualDown(out, _this._params.conv128_down);
+ out = residual(out, _this._params.conv128_1);
+ out = residual(out, _this._params.conv128_2);
+ out = residualDown(out, _this._params.conv256_down);
+ out = residual(out, _this._params.conv256_1);
+ out = residual(out, _this._params.conv256_2);
+ out = residualDown(out, _this._params.conv256_down_out);
+ var globalAvg = out.mean([1, 2]);
+ var fullyConnected = matMul(globalAvg, _this._params.fc);
+ return fullyConnected;
+ });
+ };
+ FaceRecognitionNet.prototype.computeFaceDescriptor = function (input) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var result, data;
+ return __generator$1(this, function (_a) {
+ switch (_a.label) {
+ case 0:
+ result = this.forward(input);
+ return [4 /*yield*/, result.data()];
+ case 1:
+ data = _a.sent();
+ result.dispose();
+ return [2 /*return*/, data];
+ }
+ });
+ });
+ };
+ FaceRecognitionNet.prototype.computeFaceDescriptorSync = function (input) {
+ return __awaiter$1(this, void 0, void 0, function () {
+ var result, data;
+ return __generator$1(this, function (_a) {
+ result = this.forward(input);
+ data = result.dataSync();
+ result.dispose();
+ return [2 /*return*/, data];
+ });
+ });
+ };
+ return FaceRecognitionNet;
+ }());
+
+ function faceRecognitionNet(weights) {
+ var net = new FaceRecognitionNet();
+ net.extractWeights(weights);
+ return net;
+ }
+
+ exports.euclideanDistance = euclideanDistance;
+ exports.NetInput = NetInput;
+ exports.tf = tfCore_esm;
+ exports.padToSquare = padToSquare;
+ exports.extractFaces = extractFaces;
+ exports.extractFaceTensors = extractFaceTensors;
+ exports.faceDetectionNet = faceDetectionNet;
+ exports.FaceDetectionNet = FaceDetectionNet;
+ exports.faceLandmarkNet = faceLandmarkNet;
+ exports.FaceLandmarkNet = FaceLandmarkNet;
+ exports.faceRecognitionNet = faceRecognitionNet;
+ exports.FaceRecognitionNet = FaceRecognitionNet;
+ exports.isFloat = isFloat;
+ exports.isEven = isEven;
+ exports.round = round$1;
+ exports.getElement = getElement;
+ exports.getContext2dOrThrow = getContext2dOrThrow;
+ exports.createCanvas = createCanvas;
+ exports.createCanvasFromMedia = createCanvasFromMedia;
+ exports.getMediaDimensions = getMediaDimensions;
+ exports.bufferToImage = bufferToImage;
+ exports.imageTensorToCanvas = imageTensorToCanvas;
+ exports.getDefaultDrawOptions = getDefaultDrawOptions;
+ exports.drawBox = drawBox;
+ exports.drawText = drawText;
+ exports.drawDetection = drawDetection;
+ exports.drawLandmarks = drawLandmarks;
+
+ Object.defineProperty(exports, '__esModule', { value: true });
})));
diff --git a/dist/face-api.min.js b/dist/face-api.min.js
index 63372485..36be643d 100644
--- a/dist/face-api.min.js
+++ b/dist/face-api.min.js
@@ -1 +1 @@
-!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("crypto")):"function"==typeof define&&define.amd?define(["exports","crypto"],e):e(t.faceapi=t.faceapi||{},t.crypto)}(this,function(t,e){"use strict";function n(t){return function(){for(var t=[],e=0;e0;)r=Math.random()*e|0,n=t[--e],t[e]=t[r],t[r]=n}function i(t,e,n){return Math.max(t,Math.min(e,n))}function u(t,e){return Math.random()*(e-t)+t}function s(t,e){if(!t)throw new Error(e)}function c(t,e,n){void 0===n&&(n=""),s(d(t,e),n+" Shapes "+t+" and "+e+" must match")}function l(t,e){s(t.dtype===e.dtype," The dtypes of the first("+t.dtype+") and second("+e.dtype+") input must match")}function f(t,e){if(void 0===e&&(e=[]),Array.isArray(t))for(var n=0;n1;--e)if(t%e==0)return[e,t/e];return[1,t]}function y(t,e){return e<=t.length?t:t+" ".repeat(e-t.length)}function b(t,e,n){return void 0===e&&(e=function(t){return 0}),new Promise(function(r,o){var a=0,i=function(){if(t())r();else{var u=e(++a);null!=n&&a>=n?o():setTimeout(i,u)}};setTimeout(i,0)})}function x(t){var e={};return t.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,function(t){for(var n=[],r=1;r0)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(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 E(t,e){for(var n=[],r=[],o=0,a=0;a1)throw new Error("Can't squeeze axis "+a+" since its dim '"+t[a]+"' is not 1");(null==e[o]||e[o]>a)&&1===t[a]&&(n.push(t[a]),r.push(a)),e[o]<=a&&o++}t[a]>1&&(n.push(t[a]),r.push(a))}return{newShape:n,keptDims:r}}function S(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 R(t,e){for(var n=0;n1)for(var a=0;aN){var s=Array.from(e.subarray(0,L)),c=Array.from(e.subarray(i-L,i));return["["+s.map(function(t,e){return F(t,o[e])}).join(", ")+", ..., "+c.map(function(t,e){return F(t,o[i-L+e])}).join(", ")+"]"]}return["["+Array.from(e).map(function(t,e){return F(t,o[e])}).join(", ")+"]"]}var l=n.slice(1);var f=r.slice(1);var h=r[0];var p=[];if(i>N){for(var d=0;d=-n&&t=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Q=function(){function t(){}return t.concat1d=function(e){return t.concat(e,0)},t.concat2d=function(e,n){return t.concat(e,n)},t.concat3d=function(e,n){return t.concat(e,n)},t.concat4d=function(e,n){return t.concat(e,n)},t.concat=function(t,e){void 0===e&&(e=0),s(t.length>=1,"Pass at least one tensor to concat"),o({tensors:t},"concat");var n=t[0];if(1===t.length)return n;for(var r=V(e,n.shape),a=1;a=0&&n>>0,e=(r*=e)>>>0,e+=4294967296*(r-=e)}return 2.3283064365386963e-10*(e>>>0)});n.next=function(){var t=2091639*n.s0+2.3283064365386963e-10*n.c;return n.s0=n.s1,n.s1=n.s2,n.s2=t-(n.c=0|t)},n.c=1,n.s0=r(" "),n.s1=r(" "),n.s2=r(" "),n.s0-=r(t),n.s0<0&&(n.s0+=1),n.s1-=r(t),n.s1<0&&(n.s1+=1),n.s2-=r(t),n.s2<0&&(n.s2+=1),r=null}(t),o=e&&e.state,a=n.next;return a.int32=function(){return 4294967296*n.next()|0},a.double=function(){return a()+1.1102230246251565e-16*(2097152*a()|0)},a.quick=a,o&&("object"==typeof o&&r(o,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=o:n&&n.amd?n(function(){return o}):this.alea=o}(0,t,!1)}),$=J(function(t){!function(t,e,n){function r(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e}function o(t,e){var n=new function(t){var e=this,n="";e.x=0,e.y=0,e.z=0,e.w=0,e.next=function(){var t=e.x^e.x<<11;return e.x=e.y,e.y=e.z,e.z=e.w,e.w^=e.w>>>19^t^t>>>8},t===(0|t)?e.x=t:n+=t;for(var r=0;r>>0)/4294967296};return a.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},a.int32=n.next,a.quick=a,o&&("object"==typeof o&&r(o,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=o:n&&n.amd?n(function(){return o}):this.xor128=o}(0,t,!1)}),tt=J(function(t){!function(t,e,n){function r(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e.v=t.v,e.d=t.d,e}function o(t,e){var n=new function(t){var e=this,n="";e.next=function(){var t=e.x^e.x>>>2;return e.x=e.y,e.y=e.z,e.z=e.w,e.w=e.v,(e.d=e.d+362437|0)+(e.v=e.v^e.v<<4^t^t<<1)|0},e.x=0,e.y=0,e.z=0,e.w=0,e.v=0,t===(0|t)?e.x=t:n+=t;for(var r=0;r>>4),e.next()}(t),o=e&&e.state,a=function(){return(n.next()>>>0)/4294967296};return a.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},a.int32=n.next,a.quick=a,o&&("object"==typeof o&&r(o,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=o:n&&n.amd?n(function(){return o}):this.xorwow=o}(0,t,!1)}),et=J(function(t){!function(t,e,n){function r(t,e){return e.x=t.x.slice(),e.i=t.i,e}function o(t,e){null==t&&(t=+new Date);var n=new function(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)}(t),o=e&&e.state,a=function(){return(n.next()>>>0)/4294967296};return a.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},a.int32=n.next,a.quick=a,o&&(o.x&&r(o,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=o:n&&n.amd?n(function(){return o}):this.xorshift7=o}(0,t,!1)}),nt=J(function(t){!function(t,e,n){function r(t,e){return e.i=t.i,e.w=t.w,e.X=t.X.slice(),e}function o(t,e){null==t&&(t=+new Date);var n=new function(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)}(t),o=e&&e.state,a=function(){return(n.next()>>>0)/4294967296};return a.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},a.int32=n.next,a.quick=a,o&&(o.X&&r(o,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=o:n&&n.amd?n(function(){return o}):this.xor4096=o}(0,t,!1)}),rt=J(function(t){!function(t,e,n){function r(t,e){return e.a=t.a,e.b=t.b,e.c=t.c,e.d=t.d,e}function o(t,e){var n=new function(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 a.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},a.int32=n.next,a.quick=a,o&&("object"==typeof o&&r(o,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=o:n&&n.amd?n(function(){return o}):this.tychei=o}(0,t,!1)}),ot=J(function(t){!function(n,r){var o,a=this,i=256,u=6,s="random",c=r.pow(i,u),l=r.pow(2,52),f=2*l,h=i-1;function p(t,e,p){var m=[],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"}((e=1==e?{entropy:!0}:e||{}).entropy?[t,g(n)]:null==t?function(){try{var t;return o&&(t=o.randomBytes)?t=t(i):(t=new Uint8Array(i),(a.crypto||a.msCrypto).getRandomValues(t)),g(t)}catch(t){var e=a.navigator,r=e&&e.plugins;return[+new Date,a,r,a.screen,g(n)]}}():t,3),m),b=new function(t){var e,n=t.length,r=this,o=0,a=r.i=r.j=0,u=r.S=[];n||(t=[n++]);for(;o=f;)t/=2,e/=2,n>>>=1;return(t+n)/e};return x.int32=function(){return 0|b.g(4)},x.quick=function(){return b.g(4)/4294967296},x.double=x,v(g(b.S),n),(e.pass||p||function(t,e,n,o){return o&&(o.S&&d(o,b),t.state=function(){return d(b,{})}),n?(r[s]=t,e):t})(x,y,"global"in e?e.global:this==r,e.state)}function d(t,e){return e.i=t.i,e.j=t.j,e.S=t.S.slice(),e}function v(t,e){for(var n,r=t+"",o=0;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}(),it=function(t,e,n,r){var o,a=arguments.length,i=a<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(t,e,n,r);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},ut=function(){function t(){}return t.logSumExp=function(t,e,n){void 0===e&&(e=null),void 0===n&&(n=!1),o({x:t},"logSumExp");var r=V(e,t.shape),a=t.max(r,!0),i=t.sub(a).exp().sum(r).log(),u=a.reshape(i.shape).add(i);if(n){var s=W(u.shape,r);return u.reshape(s)}return u},t.sum=function(t,e,n){void 0===e&&(e=null),void 0===n&&(n=!1),o({x:t},"sum"),"bool"===t.dtype&&(t=t.toInt());var r=V(e,t.shape);return Ko(function(t){var e=q(r,t.rank),o=r,a=t;null!=e&&(a=t.transpose(e),o=j(o.length,t.rank));var i=fa.engine.runKernel(function(t){return t.sum(a,o)},{permutedX:a});if(n){var u=W(i.shape,r);i=i.reshape(u)}return{value:i,gradFunc:function(e){var n=t.shape.slice();return r.forEach(function(t){n[t]=1}),e.reshape(n).mul(Pr(t.shape,"float32"))}}})(t)},t.mean=function(t,e,n){void 0===e&&(e=null),void 0===n&&(n=!1),o({x:t},"mean");var r=V(e,t.shape),a=p(z(t.shape,r)[1]);return Ko(function(t){var o=no(a);return{value:(o.dtype===t.dtype?t:t.cast(o.dtype)).div(o).sum(e,n),gradFunc:function(e){var n=t.shape.slice();return r.forEach(function(t){n[t]=1}),e.reshape(n).mul(Pr(t.shape,"float32")).div(o)}}})(t)},t.min=function(t,e,n){void 0===e&&(e=null),void 0===n&&(n=!1),o({x:t},"min");var r=V(e,t.shape),a=r,i=q(a,t.rank);null!=i&&(t=t.transpose(i),a=j(a.length,t.rank));var u=fa.engine.runKernel(function(e){return e.min(t,a)},{x:t});if(n){var s=W(u.shape,r);return u.reshape(s)}return u},t.max=function(t,e,n){void 0===e&&(e=null),void 0===n&&(n=!1),o({x:t},"max");var r=V(e,t.shape),a=r,i=q(a,t.rank);null!=i&&(t=t.transpose(i),a=j(a.length,t.rank));var u=fa.engine.runKernel(function(e){return e.max(t,a)},{x:t});if(n){var s=W(u.shape,r);return u.reshape(s)}return u},t.argMin=function(t,e){void 0===e&&(e=0),o({x:t},"argMin"),null==e&&(e=0);var n=V(e,t.shape),r=q(n,t.rank);return null!=r&&(t=t.transpose(r),n=j(n.length,t.rank)),fa.engine.runKernel(function(e){return e.argMin(t,n[0])},{x:t})},t.argMax=function(t,e){void 0===e&&(e=0),o({x:t},"argMax"),null==e&&(e=0);var n=V(e,t.shape),r=q(n,t.rank);return null!=r&&(t=t.transpose(r),n=j(n.length,t.rank)),fa.engine.runKernel(function(e){return e.argMax(t,n[0])},{x:t})},t.moments=function(t,e,n){void 0===e&&(e=null),void 0===n&&(n=!1),o({x:t},"moments");var r=V(e,t.shape),a=t.mean(r,n),i=a.shape;return n||(i=W(a.shape,r)),{mean:a,variance:t.toFloat().sub(a.reshape(i)).square().mean(r,n)}},t.unsortedSegmentSum=function(t,e,n,r){void 0===r&&(r=0),o({x:t,segmentIds:e},"unsortedSegmentSum"),s("int32"===e.dtype,"Segment Ids must be of dtype `int32`"),r=V(r,t.shape)[0];for(var a=[],i=e.shape[0],u=[],c=0;c=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},ct=function(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())})},lt=function(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=r[2&a[0]?"return":a[0]?"throw":"next"])&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(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]2)throw new Error("Rank of probabilities must be 1 or 2, but is "+i);n=n||Math.random();var u=1===i?t.as2D(1,-1):t,s=fa.engine.runKernel(function(t){return t.multinomial(u,r,e,n)},{logits2D:u});return 1===i?s.as1D():s},t.oneHot=function(t,e,n,r){if(void 0===n&&(n=1),void 0===r&&(r=0),s("int32"===t.dtype,"Indices must be of dtype `int32`"),e<2)throw new Error("Error in oneHot: depth must be >=2, but it is "+e);return fa.engine.runKernel(function(o){return o.oneHot(t,e,n,r)},{indices:t})},t.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.");return fa.engine.fromPixels(t,e)},t.toPixels=function(t,e){return ct(this,void 0,void 0,function(){var n,r,a,i,u,s,c,l,f,h,p,d,v,g,m,y,b,x,w;return lt(this,function(E){switch(E.label){case 0:if(o({img:t},"toPixels"),2!==t.rank&&3!==t.rank)throw new Error("toPixels only supports rank 2 or 3 tensors, got rank "+t.rank+".");if(n=t.shape.slice(0,2),r=n[0],a=n[1],(i=2===t.rank?1:t.shape[2])>4||2===i)throw new Error("toPixels only supports depth of size 1, 3 or 4 but got "+i);return u=t.min(),s=t.max(),[4,u.data()];case 1:return c=E.sent()[0],[4,s.data()];case 2:if(l=E.sent()[0],u.dispose(),s.dispose(),"float32"===t.dtype){if(c<0||l>1)throw new Error("Tensor values for a float32 Tensor must be in the range [0 - 1] but got range ["+c+" - "+l+"].")}else{if("int32"!==t.dtype)throw new Error("Unsupported type for toPixels: "+t.dtype+". Please use float32 or int32 tensors.");if(c<0||l>255)throw new Error("Tensor values for a int32 Tensor must be in the range [0 - 255] but got range ["+c+" - "+l+"].")}return[4,t.data()];case 3:for(f=E.sent(),h="float32"===t.dtype?255:1,p=new Uint8ClampedArray(a*r*4),d=0;d=1,"Pass at least one tensor to tf.stack"),1===t.length)return t[0].expandDims(e);var n=t[0].rank,r=t[0].shape,a=t[0].dtype;s(e<=n,"Axis must be <= rank of the tensor"),t.forEach(function(t){c(r,t.shape,"All tensors passed to stack must have matching shapes")}),t.forEach(function(t){s(a===t.dtype,"All tensors passed to stack must have matching dtypes")});var i=t.map(function(t){return t.expandDims(e)});return Q.concat(i,e)},t.unstack=function(t,e){void 0===e&&(e=0);for(var n,r=t.shape[e],o=Array(t.rank-1).fill(0),a=0,i=0;i1)return t.zeros([0],o);var a=ht(Math.abs(Math.ceil((n-e)/r)),o);n1&&1===i&&r.unshift(a)}return r}function dt(t,e){for(var n=[],r=0;r1)&&n.unshift(a)}return n}function vt(t,e){for(var n=[],r="Operands could not be broadcast together with shapes "+t+" and "+e+".",o=Math.max(t.length,e.length),a=0;a1&&u>1&&i!==u)throw Error(r);n.unshift(Math.max(i,u))}return n}var gt,mt,yt,bt,xt,wt=function(t,e,n,r){var o,a=arguments.length,i=a<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(t,e,n,r);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Et=function(){function t(){}return t.batchNormalization2d=function(e,n,r,o,a,i){return void 0===o&&(o=.001),s(2===e.rank,"Error in batchNormalization3D: x must be rank 3 but got rank "+e.rank+"."),s(2===n.rank||1===n.rank,"Error in batchNormalization2D: mean must be rank 2 or rank 1 but got rank "+n.rank+"."),s(2===r.rank||1===r.rank,"Error in batchNormalization2D: variance must be rank 2 or rank 1 but got rank "+r.rank+"."),null!=a&&s(2===a.rank||1===a.rank,"Error in batchNormalization2D: scale must be rank 2 or rank 1 but got rank "+a.rank+"."),null!=i&&s(2===i.rank||1===i.rank,"Error in batchNormalization2D: offset must be rank 2 or rank 1 but got rank "+i.rank+"."),t.batchNormalization(e,n,r,o,a,i)},t.batchNormalization3d=function(e,n,r,o,a,i){return void 0===o&&(o=.001),s(3===e.rank,"Error in batchNormalization3D: x must be rank 3 but got rank "+e.rank+"."),s(3===n.rank||1===n.rank,"Error in batchNormalization3D: mean must be rank 3 or rank 1 but got rank "+n.rank+"."),s(3===r.rank||1===r.rank,"Error in batchNormalization3D: variance must be rank 3 or rank 1 but got rank "+r.rank+"."),null!=a&&s(3===a.rank||1===a.rank,"Error in batchNormalization3D: scale must be rank 3 or rank 1 but got rank "+a.rank+"."),null!=i&&s(3===i.rank||1===i.rank,"Error in batchNormalization3D: offset must be rank 3 or rank 1 but got rank "+i.rank+"."),t.batchNormalization(e,n,r,o,a,i)},t.batchNormalization4d=function(e,n,r,o,a,i){return void 0===o&&(o=.001),s(4===e.rank,"Error in batchNormalization4D: x must be rank 4 but got rank "+e.rank+"."),s(4===n.rank||1===n.rank,"Error in batchNormalization4D: mean must be rank 4 or rank 1 but got rank "+n.rank+"."),s(4===r.rank||1===r.rank,"Error in batchNormalization4D: variance must be rank 4 or rank 1 but got rank "+r.rank+"."),null!=a&&s(4===a.rank||1===a.rank,"Error in batchNormalization4D: scale must be rank 4 or rank 1 but got rank "+a.rank+"."),null!=i&&s(4===i.rank||1===i.rank,"Error in batchNormalization4D: offset must be rank 4 or rank 1 but got rank "+i.rank+"."),t.batchNormalization(e,n,r,o,a,i)},t.batchNormalization=function(t,e,n,r,a,i){var u;void 0===r&&(r=.001),o({x:t,mean:e,variance:n},"batchNormalization"),null!=a&&o({scale:a},"batchNormalization"),null!=i&&o({offset:i},"batchNormalization"),s(e.rank===n.rank,"Batch normalization gradient requires mean and variance to have equal ranks."),s(null==i||e.rank===i.rank,"Batch normalization gradient requires mean and offset to have equal ranks."),s(null==a||e.rank===a.rank,"Batch normalization gradient requires mean and scale to have equal ranks."),u=0===t.rank||1===t.rank?t.as4D(1,1,1,t.size):2===t.rank?t.as4D(1,1,t.shape[0],t.shape[1]):3===t.rank?t.as4D(1,t.shape[0],t.shape[1],t.shape[2]):t;return fa.engine.runKernel(function(t){return t.batchNormalization(u,St(e),St(n),r,St(a),St(i))},{x:t,mean:e,variance:n,scale:a,offset:i},function(o){var i=null==a?ft.scalar(1):a,s=dt(e.shape,u.shape),c=[];if(1===e.rank){for(var l=0;l=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Tt=function(){function t(){}return t.add=function(t,e){o({a:t,b:e},"add"),l(t,e);var n=vt(t.shape,e.shape);return fa.engine.runKernel(function(n){return n.add(t,e)},{a:t,b:e},function(r){return{a:function(){var e=r,o=dt(t.shape,n);return o.length>0&&(e=e.sum(o)),e.reshape(t.shape)},b:function(){var t=r,o=dt(e.shape,n);return o.length>0&&(t=t.sum(o)),t.reshape(e.shape)}}})},t.addStrict=function(t,e){return c(t.shape,e.shape,"Error in addStrict: "),t.add(e)},t.sub=function(t,e){o({a:t,b:e},"sub"),l(t,e);var n=vt(t.shape,e.shape);return fa.engine.runKernel(function(n){return n.subtract(t,e)},{a:t,b:e},function(r){return{a:function(){var e=r,o=dt(t.shape,n);return o.length>0&&(e=e.sum(o)),e.reshape(t.shape)},b:function(){var t=r,o=dt(e.shape,n);return o.length>0&&(t=t.sum(o)),t.neg().reshape(e.shape)}}})},t.subStrict=function(t,e){return c(t.shape,e.shape,"Error in subStrict: "),t.sub(e)},t.pow=function(t,e){o({base:t,exp:e},"pow");var n=vt(t.shape,e.shape);t=t.cast(kt(t.dtype,e.dtype)),e=e.cast(kt(t.dtype,e.dtype));return fa.engine.runKernel(function(n,r){return r(n.pow(t,e))},{base:t,exp:e},function(r,o){var a=o[0];return{base:function(){var o=r.mul(e.toFloat().mul(a.div(t))),i=dt(t.shape,n);return i.length>0&&(o=o.sum(i)),o.reshape(t.shape)},exp:function(){var o=r.mul(a.mul(t.log()).toFloat()),i=dt(e.shape,n);return i.length>0&&(o=o.sum(i)),o.reshape(e.shape)}}})},t.powStrict=function(t,e){return c(t.shape,e.shape,"Error in powStrict: "),t.pow(e)},t.mul=function(t,e){o({a:t,b:e},"mul"),l(t,e);var n=vt(t.shape,e.shape);return fa.engine.runKernel(function(n){return n.multiply(t,e)},{a:t,b:e},function(r){return{a:function(){var o=r.mul(e.toFloat()),a=dt(t.shape,n);return a.length>0?o.sum(a).reshape(t.shape):o},b:function(){var o=r.mul(t.toFloat()),a=dt(e.shape,n);return a.length>0?o.sum(a).reshape(e.shape):o}}})},t.mulStrict=function(t,e){return c(t.shape,e.shape,"Error in multiplyStrict: "),t.mul(e)},t.div=function(t,e){o({a:t,b:e},"div"),l(t,e);var n=vt(t.shape,e.shape);return fa.engine.runKernel(function(n){return n.divide(t,e)},{a:t,b:e},function(r){return{a:function(){var o=r.div(e.toFloat()),a=dt(t.shape,n);return a.length>0?o.sum(a).reshape(t.shape):o},b:function(){var o=r.mul(t.toFloat()),a=dt(e.shape,n);a.length>0&&(o=o.sum(a).reshape(e.shape));var i=e.square();return o.div(i.toFloat()).neg()}}})},t.divStrict=function(t,e){return c(t.shape,e.shape,"Error in divideStrict: "),t.div(e)},t.mod=function(t,e){o({a:t,b:e},"mod"),l(t,e);var n=vt(t.shape,e.shape);return fa.engine.runKernel(function(n){return n.mod(t,e)},{a:t,b:e},function(r){return{a:function(){var e=dt(t.shape,n);return e.length>0?r.sum(e).reshape(t.shape):r},b:function(){var o=r.mul(t.div(e).floor().neg()),a=dt(e.shape,n);return a.length>0?o.sum(a).reshape(e.shape):o}}})},t.modStrict=function(t,e){return c(t.shape,e.shape,"Error in modStrict: "),t.mod(e)},t.minimum=function(t,e){o({a:t,b:e},"minimum"),l(t,e),"bool"===t.dtype&&(t=t.toInt()),"bool"===e.dtype&&(e=e.toInt()),vt(t.shape,e.shape);return fa.engine.runKernel(function(n){return n.minimum(t,e)},{a:t,b:e},function(n){return{a:function(){return n.mul(t.lessEqual(e).toFloat())},b:function(){return n.mul(t.greater(e).toFloat())}}})},t.minimumStrict=function(t,e){return c(t.shape,e.shape,"Error in minimumStrict: "),t.minimum(e)},t.maximum=function(t,e){o({a:t,b:e},"maximum"),l(t,e),"bool"===t.dtype&&(t=t.toInt()),"bool"===e.dtype&&(e=e.toInt()),vt(t.shape,e.shape);return fa.engine.runKernel(function(n){return n.maximum(t,e)},{a:t,b:e},function(n){return{a:function(){return n.mul(t.greaterEqual(e).toFloat())},b:function(){return n.mul(t.less(e).toFloat())}}})},t.maximumStrict=function(t,e){return c(t.shape,e.shape,"Error in minimumStrict: "),t.maximum(e)},t.squaredDifference=function(t,e){o({a:t,b:e},"squaredDifference"),l(t,e),vt(t.shape,e.shape);return fa.engine.runKernel(function(n){return n.squaredDifference(t,e)},{a:t,b:e},function(n){var r=no(2);return{a:function(){return n.mul(t.sub(e).mul(r))},b:function(){return n.mul(e.sub(t).mul(r))}}})},t.squaredDifferenceStrict=function(t,e){return c(t.shape,e.shape,"Error in squaredDifferenceStrict: "),t.squaredDifference(e)},t.atan2=function(e,n){o({a:e,b:n},"atan2"),l(e,n);var r=vt(e.shape,n.shape);return fa.engine.runKernel(function(t){return t.atan2(e,n)},{a:e,b:n},function(o){return{a:function(){var a=t.add(ur(e),ur(n)),i=o.mul(n.div(a)),u=dt(e.shape,r);return u.length>0&&(i=i.sum(u)),i.reshape(e.shape)},b:function(){var a=t.add(ur(e),ur(n)),i=Qn(o.mul(e.div(a))),u=dt(n.shape,r);return u.length>0&&(i=i.sum(u)),i.reshape(n.shape)}}})},Ct([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},At=function(){function t(){}return t.notEqual=function(t,e){return o({a:t,b:e},"notEqual"),l(t,e),vt(t.shape,e.shape),fa.engine.runKernel(function(n){return n.notEqual(t,e)},{a:t,b:e})},t.notEqualStrict=function(t,e){return c(t.shape,e.shape,"Error in notEqualStrict: "),t.notEqual(e)},t.less=function(t,e){return o({a:t,b:e},"less"),l(t,e),vt(t.shape,e.shape),fa.engine.runKernel(function(n){return n.less(t,e)},{a:t,b:e})},t.lessStrict=function(t,e){return c(t.shape,e.shape,"Error in lessStrict: "),t.less(e)},t.equal=function(t,e){return o({a:t,b:e},"equal"),l(t,e),vt(t.shape,e.shape),fa.engine.runKernel(function(n){return n.equal(t,e)},{a:t,b:e})},t.equalStrict=function(t,e){return c(t.shape,e.shape,"Error in equalStrict: "),t.equal(e)},t.lessEqual=function(t,e){return o({a:t,b:e},"lessEqual"),l(t,e),vt(t.shape,e.shape),fa.engine.runKernel(function(n){return n.lessEqual(t,e)},{a:t,b:e})},t.lessEqualStrict=function(t,e){return c(t.shape,e.shape,"Error in lessEqualStrict: "),t.lessEqual(e)},t.greater=function(t,e){return o({a:t,b:e},"greater"),l(t,e),vt(t.shape,e.shape),fa.engine.runKernel(function(n){return n.greater(t,e)},{a:t,b:e})},t.greaterStrict=function(t,e){return c(t.shape,e.shape,"Error in greaterStrict: "),t.greater(e)},t.greaterEqual=function(t,e){return o({a:t,b:e},"greaterEqual"),l(t,e),vt(t.shape,e.shape),fa.engine.runKernel(function(n){return n.greaterEqual(t,e)},{a:t,b:e})},t.greaterEqualStrict=function(t,e){return c(t.shape,e.shape,"Error in greaterEqualStrict: "),t.greaterEqual(e)},_t([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Pt=function(){function t(){}return t.conv1d=function(e,n,r,a,i,u,c){void 0===i&&(i="NWC"),void 0===u&&(u=1),o({x:e,filter:n},"conv1d");var l=e,f=!1;2===e.rank&&(f=!0,l=e.as3D(1,e.shape[0],e.shape[1])),s(3===l.rank,"Error in conv1d: input must be rank 3, but got rank "+l.rank+"."),s(3===n.rank,"Error in conv1d: filter must be rank 3, but got rank "+n.rank+"."),null!=c&&s(v(a),"Error in conv1d: pad must be an integer when using, dimRoundingMode "+c+" but got pad "+a+"."),s(l.shape[2]===n.shape[1],"Error in conv1d: depth of input ("+l.shape[2]+") must match input depth for filter "+n.shape[1]+"."),s(Ft(r,u),"Error in conv1D: Either stride or dilation must be 1.Got stride "+r+" and dilation '"+u+"'"),s("NWC"===i,"Error in conv1d: got dataFormat of "+i+" but only NWC is currently supported.");var h=n.as4D(1,n.shape[0],n.shape[1],n.shape[2]),p=l.as4D(l.shape[0],1,l.shape[1],l.shape[2]),d=[1,r],g=[1,u],m=t.conv2d(p,h,d,a,"NHWC",g,c);return f?m.as2D(m.shape[2],m.shape[3]):m.as3D(m.shape[0],m.shape[2],m.shape[3])},t.conv2d=function(e,n,r,a,i,u,c){void 0===i&&(i="NHWC"),void 0===u&&(u=[1,1]),o({x:e,filter:n},"conv2d");var l=e,f=!1;3===e.rank&&(f=!0,l=e.as4D(1,e.shape[0],e.shape[1],e.shape[2])),s(4===l.rank,"Error in conv2d: input must be rank 4, but got rank "+l.rank+"."),s(4===n.rank,"Error in conv2d: filter must be rank 4, but got rank "+n.rank+"."),null!=c&&s(v(a),"Error in conv2d: pad must be an integer when using, dimRoundingMode "+c+" but got pad "+a+"."),s(l.shape[3]===n.shape[2],"Error in conv2d: depth of input ("+l.shape[3]+") must match input depth for filter "+n.shape[2]+"."),s(Ft(r,u),"Error in conv2D: Either strides or dilations must be 1.Got strides "+r+" and dilations '"+u+"'"),s("NHWC"===i,"Error in conv2d: got dataFormat of "+i+" but only NHWC is currently supported.");var h=It(l.shape,n.shape,r,u,a,c),p=fa.engine.runKernel(function(t){return t.conv2d(l,n,h)},{x:l,filter:n},function(e){return s(Bt(u),"Error in gradient of conv2D: dilation rates greater than 1 are notyet supported in gradients. Got dilations '"+u+"'"),{x:function(){return t.conv2dDerInput(l.shape,e,n,r,a)},filter:function(){return t.conv2dDerFilter(l,e,n.shape,r,a)}}});return f?p.as3D(p.shape[1],p.shape[2],p.shape[3]):p},t.conv2dDerInput=function(t,e,n,r,a,i){o({dy:e,filter:n},"conv2dDerInput"),s(t.length===e.rank,"Length of inShape ("+t.length+") and rank of dy ("+e.rank+") must match");var u=t,c=e,l=!1;3===e.rank&&(l=!0,c=e.as4D(1,e.shape[0],e.shape[1],e.shape[2]),u=[1,t[0],t[1],t[2]]);var f=u[3],h=c.shape[3];s(4===u.length,"Error in conv2dDerInput: inShape must be length 4, but got length "+u.length+"."),s(4===c.rank,"Error in conv2dDerInput: dy must be rank 4, but got rank "+c.rank),s(4===n.rank,"Error in conv2dDerInput: filter must be rank 4, but got rank "+n.rank),s(f===n.shape[2],"Error in conv2dDerInput: depth of input ("+f+") must match input depth for filter "+n.shape[2]+"."),s(h===n.shape[3],"Error in conv2dDerInput: depth of output ("+h+") mustmatch output depth for filter "+n.shape[3]+"."),null!=i&&s(v(a),"Error in conv2dDerInput: pad must be an integer when using, dimRoundingMode "+i+" but got pad "+a+".");var p=It(u,n.shape,r,1,a,i),d=fa.engine.runKernel(function(t){return t.conv2dDerInput(c,n,p)},{dy4D:c});return l?d.as3D(d.shape[1],d.shape[2],d.shape[3]):d},t.conv2dDerFilter=function(t,e,n,r,a,i){o({x:t,dy:e},"conv2dDerFilter");var u=t;3===t.rank&&(u=t.as4D(1,t.shape[0],t.shape[1],t.shape[2]));var c=e;3===c.rank&&(c=e.as4D(1,e.shape[0],e.shape[1],e.shape[2])),s(4===u.rank,"Error in conv2dDerFilter: input must be rank 4, but got shape "+u.shape+"."),s(4===c.rank,"Error in conv2dDerFilter: dy must be rank 4, but got shape "+c.shape+"."),s(4===n.length,"Error in conv2dDerFilter: filterShape must be length 4, but got "+n+"."),s(u.shape[3]===n[2],"Error in conv2dDerFilter: depth of input "+u.shape[3]+") must match input depth in filter ("+n[2]+"."),s(c.shape[3]===n[3],"Error in conv2dDerFilter: depth of dy ("+c.shape[3]+") must match output depth for filter ("+n[3]+")."),null!=i&&s(v(a),"Error in conv2dDerFilter: pad must be an integer when using, dimRoundingMode "+i+" but got pad "+a+".");var l=It(u.shape,n,r,1,a,i);return fa.engine.runKernel(function(t){return t.conv2dDerFilter(u,c,l)},{x4D:u,dy4D:c})},t.conv2dTranspose=function(e,n,r,a,i,u){return o({x:e,filter:n},"conv2dTranspose"),t.conv2dDerInput(r,e,n,a,i,u)},t.depthwiseConv2d=function(t,e,n,r,a,i,u){void 0===a&&(a="NHWC"),void 0===i&&(i=[1,1]),o({x:t,filter:e},"depthwiseConv2d");var c=t,l=!1;3===t.rank&&(l=!0,c=t.as4D(1,t.shape[0],t.shape[1],t.shape[2])),s(4===c.rank,"Error in depthwiseConv2D: input must be rank 4, but got rank "+c.rank+"."),s(4===e.rank,"Error in depthwiseConv2D: filter must be rank 4, but got rank "+e.rank+"."),s(c.shape[3]===e.shape[2],"Error in depthwiseConv2D: number of input channels ("+c.shape[3]+") must match the inChannels dimension in filter "+e.shape[2]+"."),null==i&&(i=[1,1]),s(Ft(n,i),"Error in depthwiseConv2d: Either strides or dilations must be 1.Got strides "+n+" and dilations '"+i+"'"),null!=u&&s(v(r),"Error in depthwiseConv2D: pad must be an integer when using, dimRoundingMode "+u+" but got pad "+r+".");var f=It(c.shape,e.shape,n,i,r,u,!0),h=fa.engine.runKernel(function(t){return t.depthwiseConv2D(c,e,f)},{x4D:c,filter:e});return l?h.as3D(h.shape[1],h.shape[2],h.shape[3]):h},t.separableConv2d=function(e,n,r,a,i,u,c){void 0===u&&(u=[1,1]),void 0===c&&(c="NHWC"),o({x:e,depthwiseFilter:n,pointwiseFilter:r},"separableConv2d");var l=e,f=!1;if(3===e.rank&&(f=!0,l=e.as4D(1,e.shape[0],e.shape[1],e.shape[2])),"NCHW"===c)throw new Error("separableConv2d currently does not support dataFormat NCHW; only NHWC is supported");s(4===l.rank,"Error in separableConv2d: input must be rank 4, but got rank "+l.rank+"."),s(4===n.rank,"Error in separableConv2d: depthwise filter must be rank 4, but got rank "+n.rank+"."),s(4===r.rank,"Error in separableConv2d: pointwise filter must be rank 4, but got rank "+n.rank+"."),s(1===r.shape[0],"Error in separableConv2d: the first dimension of pointwise filter must be 1, but got "+r.shape[0]+"."),s(1===r.shape[1],"Error in separableConv2d: the second dimension of pointwise filter must be 1, but got "+r.shape[1]+".");var h=n.shape[2],p=n.shape[3];s(r.shape[2]===h*p,"Error in separableConv2d: the third dimension of pointwise filter must be "+h*p+", but got "+r.shape[2]+".");var d=t.depthwiseConv2d(l,n,a,i,c,u),v=t.conv2d(d,r,1,"valid",c);return f?v.as3D(v.shape[1],v.shape[2],v.shape[3]):v},Lt([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Wt=function(){function t(){}return t.resizeBilinear=function(t,e,n){void 0===n&&(n=!1),o({images:t},"resizeBilinear"),s(3===t.rank||4===t.rank,"Error in resizeBilinear: x must be rank 3 or 4, but got rank "+t.rank+"."),s(2===e.length,"Error in resizeBilinear: new shape must 2D, but got shape "+e+".");var r=t,a=!1;3===t.rank&&(a=!0,r=t.as4D(1,t.shape[0],t.shape[1],t.shape[2]));var i=e[0],u=e[1],c=fa.engine.runKernel(function(t,e){return t.resizeBilinear(r,i,u,n)},{batchImages:r},function(t,e){return{batchImages:function(){return fa.engine.runKernel(function(e){return e.resizeBilinearBackprop(t,r,n)},{})}}});return a?c.as3D(c.shape[1],c.shape[2],c.shape[3]):c},t.resizeNearestNeighbor=function(t,e,n){void 0===n&&(n=!1),o({images:t},"resizeNearestNeighbor"),s(3===t.rank||4===t.rank,"Error in resizeNearestNeighbor: x must be rank 3 or 4, but got rank "+t.rank+"."),s(2===e.length,"Error in resizeNearestNeighbor: new shape must 2D, but got shape "+e+"."),s("float32"===t.dtype||"int32"===t.dtype,"`images` must have `int32` or `float32` as dtype");var r=t,a=!1;3===t.rank&&(a=!0,r=t.as4D(1,t.shape[0],t.shape[1],t.shape[2]));var i=e[0],u=e[1],c=fa.engine.runKernel(function(t){return t.resizeNearestNeighbor(r,i,u,n)},{batchImages:r});return a?c.as3D(c.shape[1],c.shape[2],c.shape[3]):c},zt([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Gt=function(){function t(){}return t.tidy=function(t,e,n){void 0===n&&(n=!1);var r=null;if(null==e){if("function"!=typeof t)throw new Error("Please provide a function to tidy()");e=t}else{if("string"!=typeof t&&!(t instanceof String))throw new Error("When calling with two arguments, the first argument to tidy() must be a string");if("function"!=typeof e)throw new Error("When calling with two arguments, the 2nd argument to tidy() must be a function");r=t}fa.engine.startScope(r,n);var o=e();return o instanceof Promise&&console.error("Cannot return a Promise inside of tidy."),fa.engine.endScope(o,n),o},t.dispose=function(t){M(t).forEach(function(t){return t.dispose()})},t.keep=function(t){return fa.engine.keep(t)},t.time=function(t){return fa.engine.time(t)},Vt([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},jt=function(){function t(){}return t.gramSchmidt=function(t){var e;if(Array.isArray(t)){e=!1,s(null!=t&&t.length>0,"Gram-Schmidt process: input must not be null, undefined, or empty");for(var n=t[0].shape[0],r=1;r0)for(var n=0;n=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Xt=function(){function t(){}return t.logicalNot=function(t){return o({x:t},"logicalNot"),s("bool"===t.dtype,"Error Array must be of type bool."),fa.engine.runKernel(function(e){return e.logicalNot(t)},{x:t})},t.logicalAnd=function(t,e){return o({a:t,b:e},"logicalAnd"),s("bool"===t.dtype&&"bool"===e.dtype,"Error Array must be of type bool."),vt(t.shape,e.shape),fa.engine.runKernel(function(n){return n.logicalAnd(t,e)},{a:t,b:e})},t.logicalOr=function(t,e){return o({a:t,b:e},"logicalOr"),s("bool"===t.dtype&&"bool"===e.dtype,"Error Array must be of type bool."),vt(t.shape,e.shape),fa.engine.runKernel(function(n){return n.logicalOr(t,e)},{a:t,b:e})},t.logicalXor=function(e,n){return o({a:e,b:n},"logicalXor"),s("bool"===e.dtype&&"bool"===n.dtype,"Error Array must be of type bool."),vt(e.shape,n.shape),t.logicalOr(e,n).logicalAnd(t.logicalAnd(e,n).logicalNot())},t.where=function(t,e,n){o({condition:t,a:e,b:n},"where"),s("bool"===t.dtype||"bool"===e.dtype||"bool"===n.dtype,"Error Array must be of type bool."),c(e.shape,n.shape,"Error in where: "),1===t.rank?s(t.shape[0]===e.shape[0],"The first dimension of `a` must match the size of `condition`."):c(t.shape,n.shape,"Error in where: ");var r=kt(e.dtype,n.dtype);return fa.engine.runKernel(function(o){return o.where(t,e,n,r)},{condition:t,a:e,b:n})},Ht([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i};!function(t){t[t.NONE=0]="NONE",t[t.MEAN=1]="MEAN",t[t.SUM=2]="SUM",t[t.SUM_BY_NONZERO_WEIGHTS=3]="SUM_BY_NONZERO_WEIGHTS"}(Ut||(Ut={}));var Qt=function(){function t(){}return t.computeWeightedLoss=function(t,e,n){void 0===n&&(n=Ut.SUM_BY_NONZERO_WEIGHTS),o({losses:t},"computeWeightedLoss"),null!=e&&o({weights:e},"computeWeightedLoss");var r=null==e?t:t.mul(e);if(n===Ut.NONE)return r;if(n===Ut.SUM)return r.sum();if(n===Ut.MEAN)return null==e?r.mean():r.sum().div(e.sum());if(n===Ut.SUM_BY_NONZERO_WEIGHTS){if(null==e)return r.sum().div(no(t.size));var a=e.notEqual(no(0)).sum().toFloat();return r.sum().div(a)}throw Error("Unknown reduction: "+n)},t.absoluteDifference=function(e,n,r,a){void 0===a&&(a=Ut.SUM_BY_NONZERO_WEIGHTS),o({labels:e,predictions:n},"absoluteDifference"),null!=r&&o({weights:r},"absoluteDifference"),c(e.shape,n.shape,"Error in absoluteDifference: ");var i=e.sub(n).abs();return t.computeWeightedLoss(i,r,a)},t.meanSquaredError=function(e,n,r,a){void 0===a&&(a=Ut.SUM_BY_NONZERO_WEIGHTS),o({labels:e,predictions:n},"meanSquaredError"),null!=r&&o({weights:r},"meanSquaredError"),c(e.shape,n.shape,"Error in meanSquaredError: ");var i=e.squaredDifference(n);return t.computeWeightedLoss(i,r,a)},t.cosineDistance=function(e,n,r,a,i){void 0===i&&(i=Ut.SUM_BY_NONZERO_WEIGHTS),o({labels:e,predictions:n},"cosineDistance"),null!=a&&o({weights:a},"cosineDistance"),c(e.shape,n.shape,"Error in cosineDistance: ");var u=no(1).sub(e.mul(n).sum(r,!0));return t.computeWeightedLoss(u,a,i)},t.hingeLoss=function(e,n,r,a){void 0===a&&(a=Ut.SUM_BY_NONZERO_WEIGHTS),o({labels:e,predictions:n},"hingeLoss"),null!=r&&o({weights:r},"hingeLoss"),c(e.shape,n.shape,"Error in hingeLoss: ");var i=no(1);e=no(2).mul(e).sub(i);var u=i.sub(e.mul(n)).relu();return t.computeWeightedLoss(u,r,a)},t.logLoss=function(e,n,r,a,i){void 0===a&&(a=1e-7),void 0===i&&(i=Ut.SUM_BY_NONZERO_WEIGHTS),o({labels:e,predictions:n},"logLoss"),null!=r&&o({weights:r},"logLoss"),c(e.shape,n.shape,"Error in logLoss: ");var u=no(1),s=no(a),l=e.mul(n.add(s).log()).neg().sub(u.sub(e).mul(u.sub(n).add(s).log()));return t.computeWeightedLoss(l,r,i)},Kt([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Jt=function(){function t(){}return t.localResponseNormalization=function(t,e,n,r,a){void 0===e&&(e=5),void 0===n&&(n=1),void 0===r&&(r=1),void 0===a&&(a=.5),o({x:t},"localResponseNormalization"),s(4===t.rank||3===t.rank,"Error in localResponseNormalization: x must be rank 3 or 4 but got\n rank "+t.rank+"."),s(v(e),"Error in localResponseNormalization3D: radius must be an integer\n but got radius "+e+".");var i=t,u=!1;3===t.rank&&(u=!0,i=t.as4D(1,t.shape[0],t.shape[1],t.shape[2]));var c=fa.engine.runKernel(function(t){return t.localResponseNormalization4D(i,e,n,r,a)},{x4D:i});return u?c.as3D(c.shape[1],c.shape[2],c.shape[3]):c},Yt([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},$t=function(){function t(){}return t.multiRNNCell=function(t,e,n,r){o({data:e,c:n,h:r},"multiRNNCell");for(var a=e,i=[],u=0;u=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},ee=function(){function t(){}return t.matMul=function(t,e,n,r){void 0===n&&(n=!1),void 0===r&&(r=!1),o({a:t,b:e},"matMul");var a=n?t.shape[0]:t.shape[1],i=r?e.shape[1]:e.shape[0];s(2===t.rank&&2===e.rank,"Error in matMul: inputs must be rank 2, got ranks "+t.rank+" and "+e.rank+"."),s(a===i,"Error in matMul: inner shapes ("+a+") and ("+i+") of Tensors with shapes "+t.shape+" and "+e.shape+" and transposeA="+n+" and transposeB="+r+" must match.");return fa.engine.runKernel(function(o){return o.matMul(t,e,n,r)},{a:t,b:e},function(o){return n||r?!n&&r?{a:function(){return o.matMul(e.toFloat(),!1,!1)},b:function(){return o.matMul(t.toFloat(),!0,!1)}}:n&&!r?{a:function(){return e.toFloat().matMul(o,!1,!0)},b:function(){return t.toFloat().matMul(o,!1,!1)}}:{a:function(){return e.toFloat().matMul(o,!0,!0)},b:function(){return o.matMul(t.toFloat(),!0,!0)}}:{a:function(){return o.matMul(e.toFloat(),!1,!0)},b:function(){return t.toFloat().matMul(o,!0,!1)}}})},t.vectorTimesMatrix=function(t,e){return s(1===t.rank,"Error in vectorTimesMatrix: first input must be rank 1, but got rank "+t.rank+"."),s(2===e.rank,"Error in vectorTimesMatrix: second input must be rank 2, but got rank "+e.rank+"."),s(t.size===e.shape[0],"Error in vectorTimesMatrix: size of vector ("+t.size+") must match first dimension of matrix ("+e.shape[0]+")"),t.as2D(1,-1).matMul(e).as1D()},t.matrixTimesVector=function(t,e){return s(1===e.rank,"Error in matrixTimesVector: second input must rank 1, but got rank "+e.rank+"."),s(2===t.rank,"Error in matrixTimesVector: first input must be a rank 2, but got rank "+t.rank+"."),s(e.size===t.shape[1],"Error in matrixTimesVector: size of first rank 1 input "+e.size+" must match inner dimension of second rank 2 input, but got shape "+t.shape+"."),t.matMul(e.as2D(-1,1)).as1D()},t.dotProduct=function(t,e){return s(1===t.rank&&1===e.rank,"Error in dotProduct: inputs must be rank 1, but got ranks "+t.rank+" and "+e.rank+"."),s(t.size===e.size,"Error in dotProduct: size of inputs ("+t.size+") and ("+e.size+") must match."),t.as2D(1,-1).matMul(e.as2D(-1,1)).asScalar()},t.outerProduct=function(t,e){return s(1===t.rank&&1===e.rank,"Error in outerProduct: inputs must be rank 1, but got ranks "+t.rank+" and "+e.rank+"."),t.as2D(-1,1).matMul(e.as2D(1,-1))},te([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},re=function(){function t(){}return t.movingAverage=function(t,e,n,r,a){void 0===a&&(a=!0),o({v:t,x:e},"movingAverage"),l(t,e),s(d(t.shape,e.shape),"Shape mismatch in v and x");var i=ft.scalar(1);n="number"==typeof n?ft.scalar(n):n;var u=i.sub(n),c=e.sub(t).mul(u);return a&&(s(null!=r,"When using zeroDebias: true, step is required."),r="number"==typeof r?ft.scalar(r):r,c=c.div(i.sub(Tt.pow(n,r)))),t.add(c)},ne([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},ae=function(){function t(){}return t.norm=function(t,e,n,r){void 0===e&&(e="euclidean"),void 0===n&&(n=null),void 0===r&&(r=!1),o({x:t},"norm");var a=function t(e,n,r){void 0===r&&(r=null);if(0===e.rank)return e.abs();if(1!==e.rank&&null===r)return t(e.reshape([-1]),n,r);if(1===e.rank||"number"==typeof r||r instanceof Array&&1===r.length){if(1===n)return e.abs().sum(r);if(n===1/0)return e.abs().max(r);if(n===-1/0)return e.abs().min(r);if("euclidean"===n||2===n)return e.abs().pow(no(2,"int32")).sum(r).sqrt();throw new Error("Error in norm: invalid ord value: "+n)}if(r instanceof Array&&2===r.length){if(1===n)return e.abs().sum(r[0]).max(r[1]-1);if(n===1/0)return e.abs().sum(r[1]).max(r[0]);if(n===-1/0)return e.abs().sum(r[1]).min(r[0]);if("fro"===n||"euclidean"===n)return e.square().sum(r).sqrt();throw new Error("Error in norm: invalid ord value: "+n)}throw new Error("Error in norm: invalid axis: "+r)}(t,e,n),i=a.shape;if(r){var u=V(n,t.shape);i=W(a.shape,u)}return a.reshape(i)},oe([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},ue=function(){function t(){}return t.maxPool=function(e,n,r,a,i){o({x:e},"maxPool");var u=e,c=!1;3===e.rank&&(c=!0,u=e.as4D(1,e.shape[0],e.shape[1],e.shape[2])),s(4===u.rank,"Error in maxPool: input must be rank 4 but got rank "+u.rank+"."),null!=i&&s(v(a),"Error in maxPool: pad must be an integer when using, dimRoundingMode "+i+" but got pad "+a+".");var l=Dt(u.shape,n,r,a,i),f=fa.engine.runKernel(function(t,e){return e(t.maxPool(u,l))},{x:u},function(e,o){var i=o[0];return{x:function(){return t.maxPoolBackprop(e,u,i,n,r,a)}}});return c?f.as3D(f.shape[1],f.shape[2],f.shape[3]):f},t.maxPoolBackprop=function(t,e,n,r,a,i,u){o({dy:t,input:e,output:n},"maxPoolBackprop"),s(e.rank===t.rank,"Rank of input ("+e.rank+") does not match rank of dy ("+t.rank+")"),s(4===t.rank,"Error in maxPoolBackprop: dy must be rank 4 but got rank "+t.rank+"."),s(4===e.rank,"Error in maxPoolBackprop: input must be rank 4 but got rank "+e.rank+"."),null!=u&&s(v(i),"Error in maxPoolBackprop: pad must be an integer when using, dimRoundingMode "+u+" but got pad "+i+".");var c=Dt(e.shape,r,a,i,u);return fa.engine.runKernel(function(r){return r.maxPoolBackprop(t,e,n,c)},{dy:t,input:e})},t.avgPool=function(e,n,r,a,i){o({x:e},"avgPool"),s("float32"===e.dtype,"The input dtype to avgPool must be float32");var u=e,c=!1;3===e.rank&&(c=!0,u=e.as4D(1,e.shape[0],e.shape[1],e.shape[2])),s(4===u.rank,"Error in avgPool: x must be rank 4 but got rank "+u.rank+"."),null!=i&&s(v(a),"Error in avgPool: pad must be an integer when using, dimRoundingMode "+i+" but got pad "+a+".");var l=Dt(u.shape,n,r,a),f=fa.engine.runKernel(function(t){return t.avgPool(u,l)},{x:u},function(e){return{x:function(){return t.avgPoolBackprop(e,u,n,r,a)}}});return f=f.cast(e.dtype),c?f.as3D(f.shape[1],f.shape[2],f.shape[3]):f},t.avgPoolBackprop=function(t,e,n,r,a){o({dy:t,input:e},"avgPoolBackprop"),s(e.rank===t.rank,"Rank of input ("+e.rank+") does not match rank of dy ("+t.rank+")");var i=e,u=t,c=!1;3===e.rank&&(c=!0,i=e.as4D(1,e.shape[0],e.shape[1],e.shape[2]),u=t.as4D(1,t.shape[0],t.shape[1],t.shape[2])),s(4===u.rank,"Error in avgPoolBackprop: dy must be rank 4 but got rank "+u.rank+"."),s(4===i.rank,"Error in avgPoolBackprop: input must be rank 4 but got rank "+i.rank+".");var l=Dt(i.shape,n,r,a),f=fa.engine.runKernel(function(t){return t.avgPoolBackprop(u,i,l)},{dy4D:u,input4D:i});return c?f.as3D(f.shape[1],f.shape[2],f.shape[3]):f},ie([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},ce=function(){function t(){}return t.reverse1d=function(e){return s(1===e.rank,"Error in reverse1D: x must be rank 1 but got\n rank "+e.rank+"."),t.reverse(e,0)},t.reverse2d=function(e,n){return s(2===e.rank,"Error in reverse2D: x must be rank 2 but got\n rank "+e.rank+"."),t.reverse(e,n)},t.reverse3d=function(e,n){return s(3===e.rank,"Error in reverse3D: x must be rank 3 but got\n rank "+e.rank+"."),t.reverse(e,n)},t.reverse4d=function(e,n){return s(4===e.rank,"Error in reverse4D: x must be rank 4 but got\n rank "+e.rank+"."),t.reverse(e,n)},t.reverse=function(t,e){if(o({x:t},"reverse"),0===t.rank)return t.clone();var n=V(e,t.shape);return fa.engine.runKernel(function(e){return e.reverse(t,n)},{x:t},function(t){return{x:function(){return t.reverse(n)}}}).reshapeAs(t)},se([function(){for(var t=[],e=0;e0?o>=u[e]:o<=u[e]);o+=r[e])n+=1;return n}),[i,c]}function fe(t,e,n,r,o){var a=e[o];t&1<0?Number.MIN_SAFE_INTEGER:Number.MAX_SAFE_INTEGER);var u=r[o];return a<0&&(a+=u),a=i(0,a,u-1)}function he(t,e,n,r,o){var a=e[o];t&1<0?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER);var u=r[o];return a<0&&(a+=u),a=n[o]>0?i(0,a,u):i(-1,a,u-1)}var pe,de=function(t,e,n,r){var o,a=arguments.length,i=a<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(t,e,n,r);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},ve=function(){function t(){}return t.slice1d=function(e,n,r){return s(1===e.rank,"slice1d expects a rank-1 tensor, but got a rank-"+e.rank+" tensor"),t.slice(e,[n],[r])},t.slice2d=function(e,n,r){return s(2===e.rank,"slice1d expects a rank-2 tensor, but got a rank-"+e.rank+" tensor"),t.slice(e,n,r)},t.slice3d=function(e,n,r){return s(3===e.rank,"slice1d expects a rank-3 tensor, but got a rank-"+e.rank+" tensor"),t.slice(e,n,r)},t.slice4d=function(e,n,r){return s(4===e.rank,"slice1d expects a rank-4 tensor, but got a rank-"+e.rank+" tensor"),t.slice(e,n,r)},t.slice=function(t,e,n){if(o({x:t},"slice"),0===t.rank)throw new Error("Slicing scalar is not possible");var r,a;r="number"==typeof e?[e].concat(new Array(t.rank-1).fill(0)):e.length=0?e:(s(-1===e,"Bad value in size"),t.shape[n]-r[n])}),function(t,e,n){s(t.rank===e.length,"Error in slice"+t.rank+"D: Length of begin "+e+" must match the rank of the array ("+t.rank+")."),s(t.rank===n.length,"Error in slice"+t.rank+"D: Length of size "+n+" must match the rank of the array ("+t.rank+").");for(var r=0;r=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},me=function(){function t(){}return t.softmax=function(t,e){if(void 0===e&&(e=-1),o({logits:t},"softmax"),-1===e&&(e=t.rank-1),e!==t.rank-1)throw Error("Softmax along a non-last dimension is not yet supported. Logits was rank "+t.rank+" and dim was "+e);return Ko(function(t){var n=t.logSumExp([e],!0),r=t.toFloat().sub(n).exp();return{value:r,gradFunc:function(t){var n=t.mul(r);return n.sub(n.sum([e],!0).mul(r))}}})(t)},t.softmaxCrossEntropy=function(t,e,n){if(void 0===n&&(n=-1),o({labels:t,logits:e},"softmaxCrossEntropy"),c(t.shape,e.shape,"Error in softmaxCrossEntropy: "),-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 Ko(function(t,e){var r=e.softmax(n);return{value:no(1e-5).add(r).log().mul(t).neg().sum([n]),gradFunc:function(e){var o=W(e.shape,[n]);return[e.reshape(o).mul(t.toFloat().sub(r)),e.reshape(o).mul(r.sub(t.toFloat()))]}}})(t,e)},ge([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},be=function(){function t(){}return t.stridedSlice=function(t,e,n,r,a,i){return void 0===a&&(a=0),void 0===i&&(i=0),o({x:t},"stridedSlice"),fa.engine.runKernel(function(o){return o.stridedSlice(t,e,n,r,a,i)},{x:t})},ye([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},we=function(){function t(){}return t.transpose=function(t,e){if(o({x:t},"transpose"),null==e&&(e=t.shape.map(function(t,e){return e}).reverse()),s(t.rank===e.length,"Error in transpose: rank of input "+t.rank+" must match length of perm "+e+"."),e.forEach(function(n){s(n>=0&&n=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Se=function(){function t(){}return t.neg=function(t){o({x:t},"neg");return fa.engine.runKernel(function(e){return e.neg(t)},{x:t},function(t){return{x:function(){return t.neg()}}})},t.ceil=function(t){o({x:t},"ceil");return fa.engine.runKernel(function(e){return e.ceil(t)},{x:t},function(t){return{x:function(){return Ur(t)}}})},t.floor=function(t){o({x:t},"floor");return fa.engine.runKernel(function(e){return e.floor(t)},{x:t},function(t){return{x:function(){return Ur(t)}}})},t.sign=function(t){o({x:t},"sign");return fa.engine.runKernel(function(e){return e.sign(t)},{x:t},function(t){return{x:function(){return Ur(t)}}})},t.round=function(t){o({x:t},"round");return fa.engine.runKernel(function(e){return e.round(t)},{x:t},function(t){return{x:function(){return Ur(t)}}})},t.exp=function(t){o({x:t},"exp");return fa.engine.runKernel(function(e,n){return n(e.exp(t))},{x:t},function(t,e){var n=e[0];return{x:function(){return t.mulStrict(n)}}})},t.expm1=function(t){o({x:t},"expm1");return fa.engine.runKernel(function(e){return e.expm1(t)},{x:t},function(e){return{x:function(){return e.mulStrict(t.exp())}}})},t.log=function(t){o({x:t},"log");return fa.engine.runKernel(function(e){return e.log(t)},{x:t},function(e){return{x:function(){return e.divStrict(t.toFloat())}}})},t.log1p=function(t){o({x:t},"log1p");return fa.engine.runKernel(function(e){return e.log1p(t)},{x:t},function(e){return{x:function(){return e.divStrict(t.add(no(1)))}}})},t.sqrt=function(t){o({x:t},"sqrt");return fa.engine.runKernel(function(e){return e.sqrt(t)},{x:t},function(e){return{x:function(){return e.divStrict(t.toFloat().sqrt().mul(no(2)))}}})},t.rsqrt=function(t){o({x:t},"rsqrt");return fa.engine.runKernel(function(e){return e.rsqrt(t)},{x:t},function(e){return{x:function(){return e.divStrict(t.pow(no(1.5)).mul(no(2))).neg()}}})},t.square=function(t){o({x:t},"square");return fa.engine.runKernel(function(e){return e.square(t)},{x:t},function(e){return{x:function(){return e.mulStrict(t.toFloat().mul(no(2)))}}})},t.reciprocal=function(t){o({x:t},"reciprocal");return fa.engine.runKernel(function(e){return e.reciprocal(t)},{x:t},function(e){return{x:function(){return e.divStrict(t.square().neg())}}})},t.abs=function(t){o({x:t},"abs");return fa.engine.runKernel(function(e){return e.abs(t)},{x:t},function(e){return{x:function(){return e.mulStrict(t.toFloat().step(-1))}}})},t.clipByValue=function(t,e,n){o({x:t},"clipByValue"),s(e<=n,"Error in clip: min ("+e+") must be less than or equal to max ("+n+").");return fa.engine.runKernel(function(r){return r.clip(t,e,n)},{x:t},function(r){return{x:function(){return r.where(t.greater(no(e)).logicalAnd(t.less(no(n))),Ur(r))}}})},t.relu=function(t){if(o({x:t},"relu"),"bool"===t.dtype)return t.toInt();return fa.engine.runKernel(function(e){return e.relu(t)},{x:t},function(e){var n=t.step();return{x:function(){return e.mulStrict(n.toFloat())}}})},t.elu=function(t){o({x:t},"elu");return fa.engine.runKernel(function(e,n){return n(e.elu(t))},{x:t},function(t,e){var n=e[0];return{x:function(){return fa.engine.runKernel(function(e){return e.eluDer(t,n)},{dy:t,y:n})}}})},t.selu=function(t){o({x:t},"selu");return fa.engine.runKernel(function(e){return e.selu(t)},{x:t},function(e){return{x:function(){var n=t.greater(no(0)),r=no(1.7580993408473768),o=no(1.0507009873554805),a=e.mul(o),i=e.mul(r).mul(t.toFloat().exp());return _n(n,a,i)}}})},t.leakyRelu=function(t,e){return void 0===e&&(e=.2),o({x:t},"leakyRelu"),mr(no(e).mul(t),t)},t.prelu=function(t,e){o({x:t,alpha:e},"prelu");var n=no(0);return mr(n,t).add(e.mul(br(n,t)))},t.sigmoid=function(t){o({x:t},"sigmoid");return fa.engine.runKernel(function(e,n){return n(e.sigmoid(t))},{x:t},function(t,e){var n=e[0];return{x:function(){return t.mulStrict(n.mul(no(1).sub(n)))}}})},t.logSigmoid=function(t){o({x:t},"logSigmoid");return fa.engine.runKernel(function(e){return e.softplus(t.neg()).neg()},{x:t},function(e){return{x:function(){return e.mulStrict(t.neg().sigmoid())}}})},t.softplus=function(t){o({x:t},"softplus");return fa.engine.runKernel(function(e){return e.softplus(t)},{x:t},function(e){return{x:function(){return e.mulStrict(t.sigmoid())}}})},t.sin=function(t){o({x:t},"sin");return fa.engine.runKernel(function(e){return e.sin(t)},{x:t},function(e){return{x:function(){return t.toFloat().cos().mulStrict(e)}}})},t.cos=function(t){o({x:t},"cos");return fa.engine.runKernel(function(e){return e.cos(t)},{x:t},function(e){return{x:function(){return t.toFloat().sin().neg().mulStrict(e)}}})},t.tan=function(t){o({x:t},"tan");return fa.engine.runKernel(function(e){return e.tan(t)},{x:t},function(e){return{x:function(){return e.divStrict(t.cos().square())}}})},t.asin=function(e){o({x:e},"asin");return fa.engine.runKernel(function(t){return t.asin(e)},{x:e},function(n){return{x:function(){return n.divStrict(t.sqrt(no(1).sub(e.toFloat().square())))}}})},t.acos=function(e){o({x:e},"acos");return fa.engine.runKernel(function(t){return t.acos(e)},{x:e},function(n){return{x:function(){return n.divStrict(t.sqrt(no(1).sub(e.toFloat().square()))).neg()}}})},t.atan=function(t){o({x:t},"atan");return fa.engine.runKernel(function(e){return e.atan(t)},{x:t},function(e){return{x:function(){return e.divStrict(no(1).add(t.toFloat().square()))}}})},t.sinh=function(t){o({x:t},"sinh");return fa.engine.runKernel(function(e){return e.sinh(t)},{x:t},function(e){return{x:function(){return t.toFloat().cosh().mulStrict(e)}}})},t.cosh=function(t){o({x:t},"cosh");return fa.engine.runKernel(function(e){return e.cosh(t)},{x:t},function(e){return{x:function(){return t.toFloat().sinh().mulStrict(e)}}})},t.tanh=function(t){o({x:t},"tanh");return fa.engine.runKernel(function(e,n){return n(e.tanh(t))},{x:t},function(t,e){var n=e[0];return{x:function(){return no(1).sub(n.square()).mulStrict(t)}}})},t.asinh=function(e){o({x:e},"asinh");return fa.engine.runKernel(function(t){return t.asinh(e)},{x:e},function(n){return{x:function(){return n.divStrict(t.sqrt(no(1).add(e.toFloat().square())))}}})},t.acosh=function(e){o({x:e},"acosh");return fa.engine.runKernel(function(t){return t.acosh(e)},{x:e},function(n){return{x:function(){return n.divStrict(t.sqrt(e.toFloat().square().sub(no(1))))}}})},t.atanh=function(t){o({x:t},"atanh");return fa.engine.runKernel(function(e){return e.atanh(t)},{x:t},function(e){return{x:function(){return e.divStrict(no(1).sub(t.toFloat().square()))}}})},t.erf=function(t){s("int32"===t.dtype||"float32"===t.dtype,"Input dtype must be `int32` or `float32`."),"int32"===t.dtype&&(t=t.toFloat());return fa.engine.runKernel(function(e){return e.erf(t)},{x:t},function(e){return{x:function(){return e.mulStrict(no(2/Math.sqrt(Math.PI)).mul(t.square().neg().exp()))}}})},t.step=function(t,e){void 0===e&&(e=0),o({x:t},"step");return fa.engine.runKernel(function(n){return n.step(t,e)},{x:t},function(t){return{x:function(){return Ur(t)}}})},Ee([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Ao=function(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())})},Do=function(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=r[2&a[0]?"return":a[0]?"throw":"next"])&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(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)n[r]=n[r+1]*t[r+1];return n}var Po=function(t,e,n,r){var o,a=arguments.length,i=a<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(t,e,n,r);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Bo=function(){function t(){}return t.gradScope=function(t,e){return Uo(t,e,!0)},t.grad=function(t){return s(D(t),"The f passed in grad(f) must be a function"),function(e,n){return s(e instanceof Mo,"The x passed in grad(f)(x) must be a tensor"),s(null==n||n instanceof Mo,"The dy passed in grad(f)(x, dy) must be a tensor"),Uo(function(){var r=fa.engine.gradients(function(){return t(e)},[e],n),o=r.value,a=r.grads;return null!=n&&c(o.shape,n.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),Fo(a),a[0]})}},t.grads=function(t){return s(D(t),"The f passed in grads(f) must be a function"),function(e,n){return s(Array.isArray(e)&&e.every(function(t){return t instanceof Mo}),"The args passed in grads(f)(args) must be an array of tensors"),s(null==n||n instanceof Mo,"The dy passed in grads(f)(args, dy) must be a tensor"),Uo(function(){var r=fa.engine.gradients(function(){return t.apply(void 0,e)},e,n),o=r.value,a=r.grads;return null!=n&&c(o.shape,n.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Fo(a),a})}},t.valueAndGrad=function(t){return s(D(t),"The f passed in valueAndGrad(f) must be a function"),function(e,n){s(e instanceof Mo,"The x passed in valueAndGrad(f)(x) must be a tensor"),s(null==n||n instanceof Mo,"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");var r=fa.engine.gradients(function(){return t(e)},[e],n),o=r.grads,a=r.value;return Fo(o),{grad:o[0],value:a}}},t.valueAndGrads=function(t){return s(D(t),"The f passed in valueAndGrads(f) must be a function"),function(e,n){s(Array.isArray(e)&&e.every(function(t){return t instanceof Mo}),"The args passed in valueAndGrads(f)(args) must be array of tensors"),s(null==n||n instanceof Mo,"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");var r=fa.engine.gradients(function(){return t.apply(void 0,e)},e,n);return null!=n&&c(r.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Fo(r.grads),r}},t.variableGrads=function(t,e){if(s(D(t),"The f passed in variableGrads(f) must be a function"),s(null==e||Array.isArray(e)&&e.every(function(t){return t instanceof Oo}),"The varList passed in variableGrads(f, varList) must be an array of variables"),null==e)for(var n in e=[],fa.engine.registeredVariables)e.push(fa.engine.registeredVariables[n]);var r=e.length;s((e=e.filter(function(t){return t.trainable})).length>0,"variableGrads() expects at least one of the input variables to be trainable, but none of the "+r+" variables is trainable.");var o=fa.engine.gradients(t,e,null,!0),a=o.value,i=o.grads;s(i.some(function(t){return null!=t}),"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()."),s(0===a.rank,"The f passed in variableGrads(f) must return a scalar, but it returned a rank-"+a.rank+" tensor");var u={};return e.forEach(function(t,e){null!=i[e]&&(u[t.name]=i[e])}),{value:a,grads:u}},t.customGrad=function(t){return fa.engine.customGrad(t)},Po([function(){for(var t=[],e=0;e0)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 Uo=Gt.tidy,zo=Gt.keep,Wo=Gt.dispose,Vo=Gt.time,Go=Bo.grad,qo=Bo.valueAndGrad,jo=Bo.grads,Ho=Bo.valueAndGrads,Xo=Bo.variableGrads,Ko=Bo.customGrad,Qo=function(){function t(t,e){this.backendTimer=t,this.logger=e,null==e&&(this.logger=new Yo)}return t.prototype.profileKernel=function(t,e){var n,r=this,o=this.backendTimer.time(function(){n=e()}),a=n.dataSync();return k(a,n.dtype,t),o.then(function(e){r.logger.logKernelProfile(t,n,a,e.kernelMs)}),n},t}(),Yo=function(){function t(){}return t.prototype.logKernelProfile=function(t,e,n,r){var o=y(r+"ms",9),a=y(t,25),i=e.rank,u=e.size,s=y(e.shape.toString(),14);console.log("%c"+a+"\t%c"+o+"\t%c"+i+"D "+s+"\t%c"+u,"font-weight:bold","color:red","color:blue","color: orange")},t}();var Jo,Zo=function(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())})},$o=function(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=r[2&a[0]?"return":a[0]?"throw":"next"])&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(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] {...}) to avoid memory leaks.");return this.activeScope.keep.push(t),t},t.prototype.startScope=function(t,e){void 0===e&&(e=!1),e&&0===this.gradientScopeCount&&(this.activeTape=[]),e&&this.gradientScopeCount++;var n={keep:[],track:[]};t&&(n.name=t),this.scopeStack.push(n),this.activeScope=n},t.prototype.endScope=function(t,e){var n=this;void 0===e&&(e=!1),e&&(this.gradientScopeCount--,0===this.gradientScopeCount&&(this.activeTape=null));var r=this.activeScope.keep,o=I(t);r=r.concat(o);for(var a=0;a0,"gradients() received an empty list of xs."),Uo("gradients",function(){var a=t();s(a instanceof Mo,"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--){i=(v=t[a]).inputs;var p=[];for(p.push(v.output),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.");var u={};return u[a.id]=null==n?Pr(a.shape):n,function(t,e){for(var n=e.length-1;n>=0;n--){var r=e[n],o=t[r.output.id];if(null==r.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+r.name+".");var a=r.gradient(o);for(var i in r.inputs){if(!(i in a))throw new Error("Cannot backprop through input "+i+". Available gradients found: "+Object.keys(a)+".");var u=a[i](),s=r.inputs[i];if(!d(u.shape,s.shape))throw new Error("Error in gradient for op "+r.name+". The gradient of input '"+i+"' has shape '"+u.shape+"', which does not match the shape of the input '"+s.shape+"'");if(null==t[s.id])t[s.id]=u;else{var c=t[s.id];t[s.id]=c.add(u),c.dispose()}}}}(u,i),{value:a,grads:e.map(function(t){return u[t.id]})}},!0)},t.prototype.customGrad=function(t){var e=this;return s(D(t),"The f passed in customGrad(f) must be a function."),function(){for(var n,r=[],o=0;o {op();...}); to avoid memory leaks.");return this.activeScope.track.push(t),t},t}(),ea=function(t,e,n,r){var o,a=arguments.length,i=a<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(t,e,n,r);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i};!function(t){t[t.NUMBER=0]="NUMBER",t[t.BOOLEAN=1]="BOOLEAN",t[t.STRING=2]="STRING"}(Jo||(Jo={}));var na=[{name:"DEBUG",type:Jo.BOOLEAN},{name:"IS_BROWSER",type:Jo.BOOLEAN},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",type:Jo.NUMBER},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",type:Jo.BOOLEAN},{name:"WEBGL_VERSION",type:Jo.NUMBER},{name:"WEBGL_FLOAT_TEXTURE_ENABLED",type:Jo.BOOLEAN},{name:"WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED",type:Jo.BOOLEAN},{name:"BACKEND",type:Jo.STRING}];function ra(t,e){return null!=t.getExtension(e)}function oa(t){if(0===t)throw new Error("Cannot get WebGL rendering context, WebGL is disabled.");var e=document.createElement("canvas");return 1===t?e.getContext("webgl")||e.getContext("experimental-webgl"):e.getContext("webgl2")}function aa(t){if(null!=t){var e=t.getExtension("WEBGL_lose_context");if(null==e)throw new Error("Extension WEBGL_lose_context not supported on this browser.");e.loseContext()}}function ia(t){var e=oa(t);return null!=e&&(aa(e),!0)}var ua=function(){function t(t){this.features={},this.registry={},null!=t&&(this.features=t),this.get("DEBUG")&&console.warn("Debugging mode is ON. The output of every math call will be downloaded to CPU and checked for NaNs. This significantly impacts performance.")}return t.setBackend=function(t,e){if(void 0===e&&(e=!1),!(t in fa.registry))throw new Error("Backend type '"+t+"' not found in registry");fa.initBackend(t,e)},t.getBackend=function(){return fa.initDefaultBackend(),fa.currentBackend},t.disposeVariables=function(){fa.engine.disposeVariables()},t.memory=function(){return fa.engine.memory()},t.prototype.get=function(t){return t in this.features?this.features[t]:(this.features[t]=this.evaluateFeature(t),this.features[t])},t.prototype.set=function(t,e){this.features[t]=e},t.prototype.getBestBackendType=function(){var t=this;if(0===Object.keys(this.registry).length)throw new Error("No backend found in registry.");return Object.keys(this.registry).map(function(e){return{name:e,entry:t.registry[e]}}).sort(function(t,e){return e.entry.priority-t.entry.priority})[0].name},t.prototype.evaluateFeature=function(t){if("DEBUG"===t)return!1;if("IS_BROWSER"===t)return"undefined"!=typeof window;if("BACKEND"===t)return this.getBestBackendType();if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"===t){var e=this.get("WEBGL_VERSION");return 0===e?0:function(t){if(0===t)return 0;var e,n=oa(t);return e=ra(n,"EXT_disjoint_timer_query_webgl2")&&2===t?2:ra(n,"EXT_disjoint_timer_query")?1:0,null!=n&&aa(n),e}(e)}if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE"===t)return this.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0&&(n=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(n)||/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(n.substr(0,4))));if("WEBGL_VERSION"===t)return ia(2)?2:ia(1)?1:0;if("WEBGL_FLOAT_TEXTURE_ENABLED"===t)return function(t){if(0===t)return!1;var e=oa(t);if(1===t){if(!ra(e,"OES_texture_float"))return!1}else if(!ra(e,"EXT_color_buffer_float"))return!1;var n=e.createFramebuffer(),r=e.createTexture();e.bindTexture(e.TEXTURE_2D,r);var o=2===t?e.RGBA32F:e.RGBA;e.texImage2D(e.TEXTURE_2D,0,o,1,1,0,e.RGBA,e.FLOAT,null),e.bindFramebuffer(e.FRAMEBUFFER,n),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,r,0);var a=e.checkFramebufferStatus(e.FRAMEBUFFER)===e.FRAMEBUFFER_COMPLETE;e.readPixels(0,0,1,1,e.RGBA,e.FLOAT,new Float32Array(4));var i=e.getError()===e.NO_ERROR;return aa(e),a&&i}(this.get("WEBGL_VERSION"));if("WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED"===t)return function(t){if(t>0)return!1;if(2!==t)return!1;var e=oa(t),n=ra(e,"WEBGL_get_buffer_sub_data_async");return aa(e),n}(this.get("WEBGL_VERSION"));var n;throw new Error("Unknown feature "+t+".")},t.prototype.setFeatures=function(t){this.features=t},t.prototype.reset=function(){this.features=ca(),null!=this.globalEngine&&(this.globalEngine.dispose(),this.globalEngine=null)},t.prototype.initBackend=function(t,e){void 0===e&&(e=!1),this.currentBackend=t,null!=this.globalEngine&&this.globalEngine.dispose();var n=fa.findBackend(t);this.globalEngine=new ta(n,e)},t.prototype.findBackend=function(t){return t in this.registry?this.registry[t].backend:null},t.prototype.registerBackend=function(t,e,n){void 0===n&&(n=1),t in this.registry&&console.warn(t+" backend was already registered");try{var r=e();return this.registry[t]={backend:r,priority:n},!0}catch(t){return console.warn(t.message),!1}},t.prototype.removeBackend=function(t){if(!(t in this.registry))throw new Error(t+" backend not found in registry");this.registry[t].backend.dispose(),delete this.registry[t]},Object.defineProperty(t.prototype,"engine",{get:function(){return this.initDefaultBackend(),this.globalEngine},enumerable:!0,configurable:!0}),t.prototype.initDefaultBackend=function(){null==this.globalEngine&&this.initBackend(fa.get("BACKEND"),!1)},ea([function(){for(var t=[],e=0;e":"<",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 = 0;\n float bestValue = getA(batch, inOffset);\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 "}}(),ba=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=e-1-t.padInfo.top,i=n-1-t.padInfo.left,u=1/(e*n);this.userCode="\n const ivec2 pads = ivec2("+a+", "+i+");\n const float avgMultiplier = float("+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 < "+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 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 float dyValue = getDy(b, idyR, idyC, d);\n\n dotProd += dyValue * avgMultiplier;\n }\n }\n setOutput(dotProd);\n }\n "}}(),xa=function(){return function(t,e,n,r,o,a){this.outputShape=[],this.supportsBroadcasting=!0,this.variableNames=["x","mean","variance"],vt(t,e),vt(t,n);var i="0.0";null!=r&&(vt(t,r),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");var u="1.0";null!=o&&(vt(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((x - mean) * inv + offset);\n }\n "}}(),wa=function(){return function(t,e,n){this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.outputShape=vt(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 "}}(),Ea=function(){return function(t,e,n){this.variableNames=["A"],this.outputShape=t;var r=e.toFixed(20),o=n.toFixed(20);this.userCode="\n void main() {\n float value = getAAtOutCoords();\n if (isNaN(value)) {\n setOutput(value);\n return;\n }\n\n setOutput(clamp(value, "+r+", "+o+"));\n }\n "}}(),Sa=function(){return function(t,e){this.variableNames=["A","B"],this.outputShape=[],this.outputShape=H(t,e,1),this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int yR = coords.x;\n int yC = coords.y;\n\n float value = 0.0;\n if (yC < "+t[1]+") {\n value = getA(yR, yC);\n } else {\n yC -= "+t[1]+";\n value = getB(yR, yC);\n }\n\n setOutput(value);\n }\n "}}(),Ra=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;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 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 "}}(),ka=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;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\n ivec2 dyCorner = coords.yz - 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 float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n "}}(),Ca=function(){return function(t){this.variableNames=["x","W"],this.outputShape=t.outShape;var e=t.padInfo.top,n=t.padInfo.left,r=t.strideHeight,o=t.strideWidth,a=t.dilationHeight,i=t.dilationWidth,u=t.filterHeight,s=t.filterWidth,c=4*Math.floor(t.inChannels/4),l=t.inChannels%4;this.userCode="\n const ivec2 strides = ivec2("+r+", "+o+");\n const ivec2 pads = ivec2("+e+", "+n+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d2 = coords[3];\n\n ivec2 xRCCorner = coords.yz * 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 < "+u+"; wR++) {\n int xR = xRCorner + wR * "+a+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+s+"; wC++) {\n int xC = xCCorner + wC * "+i+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n for (int d1 = 0; d1 < "+c+"; d1 += 4) {\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 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 dotProd += dot(xValues, wValues);\n }\n\n if ("+(1===l)+") {\n dotProd +=\n getX(batch, xR, xC, "+c+") *\n getW(wR, wC, "+c+", d2);\n } else if ("+(2===l)+") {\n vec2 xValues = vec2(\n getX(batch, xR, xC, "+c+"),\n getX(batch, xR, xC, "+c+" + 1)\n );\n vec2 wValues = vec2(\n getW(wR, wC, "+c+", d2),\n getW(wR, wC, "+c+" + 1, d2)\n );\n dotProd += dot(xValues, wValues);\n } else if ("+(3===l)+") {\n vec3 xValues = vec3(\n getX(batch, xR, xC, "+c+"),\n getX(batch, xR, xC, "+c+" + 1),\n getX(batch, xR, xC, "+c+" + 2)\n );\n vec3 wValues = vec3(\n getW(wR, wC, "+c+", d2),\n getW(wR, wC, "+c+" + 1, d2),\n getW(wR, wC, "+c+" + 2, d2)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n }\n setOutput(dotProd);\n }\n "}}();function Ta(t,e){return[e,t]}function _a(t,e){return t*e}!function(t){t[t.FLOAT=0]="FLOAT",t[t.UNSIGNED_BYTE=1]="UNSIGNED_BYTE"}(ma||(ma={}));var Aa=-2e4,Da=(2e4-Aa)/255,Ia=[1,1/255,1/65025,1/16581375],Ma=[1,255,65025],Oa=0;function Na(t,e,n){var r=function(t,e){if(t%e!=0)throw new Error("unpackedSize ("+t+") must be a multiple of "+e);return t/e}(t.length,n);if(e.length= "+r);for(var o=0,a=0;as||u.length>0),f=function(t){for(var e=0;e=1?"coords = 0;":s.map(function(t){return"coords["+(t+c)+"] = 0;"}).join("\n");var l="";l=a<2&&o>0?"coords":t.shapeInfo.logicalShape.map(function(t,e){return"coords["+(e+c)+"]"}).join(", ");return"\n float "+r+"() {\n "+i+" coords = getOutputCoords();\n "+u+"\n return get"+n+"("+l+");\n }\n "}(t,e,a,i);var h=e.texShape;if(d(r,h))return"\n float "+i+"() {\n return sampleTexture("+o+", resultUV);\n }\n ";var v=p(r),g="";l&&f&&(g="\n int mainPart = index / "+v+";\n index -= mainPart * "+v+";\n ");return"\n float "+i+"() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+h[0]+", "+h[1]+"));\n int index = resTexRC.x * "+h[1]+" + resTexRC.y;\n "+g+"\n int texR = index / "+r[1]+";\n int texC = index - texR * "+r[1]+";\n vec2 uv = (vec2(texC, texR) + halfCR) /\n vec2("+r[1]+".0, "+r[0]+".0);\n\n return sampleTexture("+o+", uv);\n }\n "}(t,e,n));return r}(t,e,r)}).join("\n"),s=e.texShape,c=function(t,e){switch(t.length){case 0:return"\n int getOutputCoords() {\n return 0;\n }\n ";case 1:return function(t,e){if(1===e[0])return"\n int getOutputCoords() {\n return int(resultUV.x * "+e[1]+".0);\n }\n ";if(1===e[1])return"\n int getOutputCoords() {\n return int(resultUV.y * "+e[0]+".0);\n }\n ";return"\n int getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+e[0]+", "+e[1]+"));\n return resTexRC.x * "+e[1]+" + resTexRC.y;\n }\n "}(0,e);case 2:return function(t,e){if(d(t,e))return"\n ivec2 getOutputCoords() {\n return ivec2(resultUV.yx * vec2("+e[0]+", "+e[1]+"));\n }\n ";if(1===t[1])return"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+e[0]+", "+e[1]+"));\n int index = resTexRC.x * "+e[1]+" + resTexRC.y;\n return ivec2(index, 0);\n }\n ";if(1===t[0])return"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+e[0]+", "+e[1]+"));\n int index = resTexRC.x * "+e[1]+" + resTexRC.y;\n return ivec2(0, index);\n }\n ";return"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+e[0]+", "+e[1]+"));\n int index = resTexRC.x * "+e[1]+" + resTexRC.y;\n int r = index / "+t[1]+";\n int c = index - r * "+t[1]+";\n return ivec2(r, c);\n }\n "}(t,e);case 3:return r=e,o=(n=t)[1]*n[2],a=n[2],"\n ivec3 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+r[0]+", "+r[1]+"));\n int index = resTexRC.x * "+r[1]+" + resTexRC.y;\n int r = index / "+o+";\n index -= r * "+o+";\n int c = index / "+a+";\n int d = index - c * "+a+";\n return ivec3(r, c, d);\n }\n ";case 4:return function(t,e){var n=t[3],r=t[2]*n,o=t[1]*r;return"\n ivec4 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+e[0]+", "+e[1]+"));\n int index = resTexRC.x * "+e[1]+" + resTexRC.y;\n\n int r = index / "+o+";\n index -= r * "+o+";\n\n int c = index / "+r+";\n index -= c * "+r+";\n\n int d = index / "+n+";\n int d2 = index - d * "+n+";\n\n return ivec4(r, c, d, d2);\n }\n "}(t,e);default:throw new Error(t.length+"-D output sampling is not yet supported")}var n,r,o,a}(e.logicalShape,s);return[Ga,o,a,i,c,u,n].join("\n")}function Fa(t){var e,n=t.shapeInfo.logicalShape;switch(n.length){case 0:return"\n float get"+(e=t.name).charAt(0).toUpperCase()+e.slice(1)+"() {\n return sampleTexture("+e+", halfCR);\n }\n ";case 1:return function(t){var e=t.name,n="get"+e.charAt(0).toUpperCase()+e.slice(1);return"\n float "+n+"(int index) {\n return "+n+"Flat(index);\n }\n "}(t);case 2:return function(t){var e=t.shapeInfo.logicalShape,n=t.shapeInfo.texShape,r=t.name,o="get"+r.charAt(0).toUpperCase()+r.slice(1),a=n[0],i=n[1];if(d(e,n))return"\n float "+o+"(int row, int col) {\n vec2 uv = (vec2(col, row) + halfCR) / vec2("+i+".0, "+a+".0);\n return sampleTexture("+r+", uv);\n }\n ";var u=E(e),s=u.newShape,c=u.keptDims,l=s;if(l.length";this.userCode="\n int getIndex(int i) {\n "+l+"\n }\n\n void main() {\n "+a+" coords = getOutputCoords();\n "+a+" adjustableCoords = "+a+"("+i+");\n int finalCoord = int("+s+");\n float val = 0.0;\n for (int i = "+o+" - 1; i >= 0; i -= 1) {\n int idx = getIndex(i);\n if (idx "+f+" finalCoord) {\n continue;\n }\n if (idx == finalCoord && "+e+") {\n continue;\n }\n "+c+" = idx;\n val += getX("+u+");\n }\n setOutput(val);\n }\n "}}();function Ka(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")}function Qa(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 Ya=function(){return function(t){this.variableNames=["x","W"],this.outputShape=t.outShape;var e=t.inHeight,n=t.inWidth,r=t.padInfo.top,o=t.padInfo.left,a=t.strideHeight,i=t.strideWidth,u=t.dilationHeight,s=t.dilationWidth,c=t.filterHeight,l=t.filterWidth,f=t.outChannels/t.inChannels;this.userCode="\n const ivec2 strides = ivec2("+a+", "+i+");\n const ivec2 pads = ivec2("+r+", "+o+");\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 / "+f+";\n int q = d2 - d1 * "+f+";\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 // TODO(dsmilkov): Flatten the two for loops and vec4 the operations.\n for (int wR = 0; wR < "+c+"; wR++) {\n int xR = xRCorner + wR * "+u+";\n\n if (xR < 0 || xR >= "+e+") {\n continue;\n }\n\n for (int wC = 0; wC < "+l+"; wC++) {\n int xC = xCCorner + wC * "+s+";\n\n if (xC < 0 || xC >= "+n+") {\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 setOutput(dotProd);\n }\n "}}(),Ja=function(){return function(t){this.variableNames=["A"];var e=t[0],n=t[1];this.outputShape=t,this.userCode="\n void main() {\n ivec3 coords = getOutputCoords();\n int texR = coords[0];\n int texC = coords[1];\n int depth = coords[2];\n vec2 uv = (vec2(texC, texR) + halfCR) / vec2("+n+".0, "+e+".0);\n\n vec4 values = texture2D(A, uv);\n float value;\n if (depth == 0) {\n value = values.r;\n } else if (depth == 1) {\n value = values.g;\n } else if (depth == 2) {\n value = values.b;\n } else if (depth == 3) {\n value = values.a;\n }\n\n setOutput(floor(value * 255.0 + 0.5));\n }\n "}}(),Za=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=qa(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;ar||n>r){o="["+e+"x"+n+"]";throw new Error("Requested texture size "+o+" greater than WebGL maximum on this browser / GPU "+("["+r+"x"+r+"]")+".")}}function xi(t){return Di(t,function(){return t.createFramebuffer()},"Unable to create WebGLFramebuffer.")}function wi(t,e,n,r,o,a,i){var u=t.getAttribLocation(e,n);return-1!==u&&(ni(t,function(){return t.bindBuffer(t.ARRAY_BUFFER,r)}),ni(t,function(){return t.vertexAttribPointer(u,o,t.FLOAT,!1,a,i)}),ni(t,function(){return t.enableVertexAttribArray(u)}),!0)}function Ei(t,e,n){Ii(t,n),ni(t,function(){return t.activeTexture(t.TEXTURE0+n)}),ni(t,function(){return t.bindTexture(t.TEXTURE_2D,e)})}function Si(t,e,n){return Di(t,function(){return t.getUniformLocation(e,n)},'uniform "'+n+'" not present in program.')}function Ri(t,e,n){return t.getUniformLocation(e,n)}function ki(t,e,n,r,o){ni(t,function(){return Ei(t,n,o)}),ni(t,function(){return t.uniform1i(r,o)})}function Ci(t,e,n){ni(t,function(){return t.bindFramebuffer(t.FRAMEBUFFER,n)}),ni(t,function(){return t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,e,0)})}function Ti(t,e){ni(t,function(){return t.bindFramebuffer(t.FRAMEBUFFER,e)}),ni(t,function(){return t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,null,0)})}function _i(t){var e=t.checkFramebufferStatus(t.FRAMEBUFFER);if(e!==t.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+Ai(t,e))}function Ai(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 Di(t,e,n){var r=ni(t,function(){return e()});if(null==r)throw new Error(n);return r}function Ii(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 Mi(t,e){2!==e.length&&(e=E(e).newShape);var n=gi(t),r=p(e);return e.length<=1&&r<=n?[r,1]:2===e.length&&e[0]<=n&&e[1]<=n?e:3===e.length&&e[0]<=n&&e[1]*e[2]<=n?[e[0],e[1]*e[2]]:4===e.length&&e[0]<=n&&e[1]*e[2]*e[3]<=n?[e[0],e[1]*e[2]*e[3]]:m(r)}var Oi=Object.freeze({createWebGLRenderingContext:ti,createWebGLRenderingContextFromCanvas:ei,callAndCheck:ni,enableDebugWebGLErrorChecking:oi,checkWebGLError:ai,getWebGLErrorMessage:ii,getExtensionOrThrow:ui,createVertexShader:si,createFragmentShader:ci,createProgram:fi,linkProgram:hi,validateProgram:pi,createStaticVertexBuffer:di,createStaticIndexBuffer:vi,queryMaxTextureSize:gi,getChannelsPerTexture:mi,createTexture:yi,validateTextureSize:bi,createFramebuffer:xi,bindVertexBufferToProgramAttribute:wi,bindTextureUnit:Ei,unbindTextureUnit:function(t,e){Ii(t,e),ni(t,function(){return t.activeTexture(t.TEXTURE0+e)}),ni(t,function(){return t.bindTexture(t.TEXTURE_2D,null)})},getProgramUniformLocationOrThrow:Si,getProgramUniformLocation:Ri,bindTextureToProgramUniformSampler:ki,bindCanvasToFramebuffer:function(t){ni(t,function(){return t.bindFramebuffer(t.FRAMEBUFFER,null)}),ni(t,function(){return t.viewport(0,0,t.canvas.width,t.canvas.height)}),ni(t,function(){return t.scissor(0,0,t.canvas.width,t.canvas.height)})},bindColorTextureToFramebuffer:Ci,unbindColorTextureFromFramebuffer:Ti,validateFramebuffer:_i,getFramebufferErrorMessage:Ai,getTextureShapeFromLogicalShape:Mi}),Ni=function(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())})},Li=function(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=r[2&a[0]?"return":a[0]?"throw":"next"])&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(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]= "+r);for(var o=0,a=0;a= "+o);for(var a=La(e,n),i=a[0],u=a[1],s=n%2==1,c=e%2==1,l=Math.floor(n/2),f=Math.floor(e/2),h=s?4:0,p=n,d=0,v=0;v= "+r);for(var o=0,a=0;a= "+o);for(var a=n%2==1,i=e%2==1,u=Math.floor(n/2),s=Math.floor(e/2),c=La(e,n),l=c[0],f=c[1],h=a?4:0,p=n+(a?1:0),d=0,v=0,g=n,m=0;m0&&o[o.length-1])&&(6===a[0]||2===a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]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 "}}(),fu=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 "}}(),hu=function(){return function(t){this.variableNames=["dy","maxPos"],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=e*n-1;this.userCode="\n const ivec2 pads = ivec2("+a+", "+i+");\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 < "+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 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 float dyValue = getDy(b, idyR, idyC, d);\n int maxPosValue = "+u+" - int(getMaxPos(b, idyR, idyC, d));\n\n // Get the current value, check it against the value from the\n // position matrix.\n int curPosValue = wR * "+n+" + wC;\n float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);\n\n dotProd += dyValue * mask;\n }\n }\n setOutput(dotProd);\n }\n "}}(),pu=function(){return function(t,e,n,r){void 0===n&&(n=!1),void 0===r&&(r=!1),this.variableNames=["matrixA","matrixB"];var o=n?t[1]:t[0],a=r?e[0]:e[1],i=n?t[0]:t[1];this.outputShape=[o,a];var u=function(t,e){return n?e+" + "+t+", aRow":"aRow, "+e+" + "+t},s=function(t,e){return r?"bCol, "+e+" + "+t:e+" + "+t+", bCol"},c=4*Math.floor(i/4),l=i%4;this.userCode=" float dotARowBCol(int aRow, int bCol) {\n float result = 0.0;\n for (int i = 0; i < "+c+"; i += 4) {\n vec4 a = vec4(\n getMatrixA("+u(0,"i")+"),\n getMatrixA("+u(1,"i")+"),\n getMatrixA("+u(2,"i")+"),\n getMatrixA("+u(3,"i")+")\n );\n vec4 b = vec4(\n getMatrixB("+s(0,"i")+"),\n getMatrixB("+s(1,"i")+"),\n getMatrixB("+s(2,"i")+"),\n getMatrixB("+s(3,"i")+")\n );\n\n result += dot(a, b);\n }\n\n if ("+(1===l)+") {\n result += getMatrixA("+u(0,c)+") *\n getMatrixB("+s(0,c)+");\n } else if ("+(2===l)+") {\n vec2 a = vec2(\n getMatrixA("+u(0,c)+"),\n getMatrixA("+u(1,c)+")\n );\n vec2 b = vec2(\n getMatrixB("+s(0,c)+"),\n getMatrixB("+s(1,c)+")\n );\n result += dot(a, b);\n } else if ("+(3===l)+") {\n vec3 a = vec3(\n getMatrixA("+u(0,c)+"),\n getMatrixA("+u(1,c)+"),\n getMatrixA("+u(2,c)+")\n );\n vec3 b = vec3(\n getMatrixB("+s(0,c)+"),\n getMatrixB("+s(1,c)+"),\n getMatrixB("+s(2,c)+")\n );\n result += dot(a, b);\n }\n\n return result;\n }\n\n void main() {\n ivec2 resRC = getOutputCoords();\n setOutput(dotARowBCol(resRC.x, resRC.y));\n }\n "}}(),du=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}(),vu=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 "}}(),gu=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=qa(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 "}}(),mu=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.filterHeight,o=t.filterWidth,a=t.strideHeight,i=t.strideWidth,u=t.padInfo.top,s=t.padInfo.left;this.outputShape=t.outShape;var c="avg"===e,l="0.0";if(c||(l="-1.0 / 0.0"),n)this.userCode="\n const ivec2 strides = ivec2("+a+", "+i+");\n const ivec2 pads = ivec2("+u+", "+s+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n float minMaxValue = 0.0;\n float minMaxValueFound = 0.0;\n int minMaxPosition = 0;\n float avgValue = 0.0;\n\n for (int wR = 0; wR < "+r+"; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+o+"; wC++) {\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 * "+o+" + wC;\n }\n }\n }\n setOutput(float(minMaxPosition));\n }\n ";else{var f=e+"("+e+"("+e+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"avg"===e&&(f="avgValue / count");var h=4*Math.floor(o/4),p=o%4,d="\n if ("+c+") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = max(values, minMaxValue);\n }\n ";this.userCode="\n const ivec2 strides = ivec2("+a+", "+i+");\n const ivec2 pads = ivec2("+u+", "+s+");\n const float initializationValue = "+l+";\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("+l+");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wR = 0; wR < "+r+"; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+h+"; wC += 4) {\n int xC = xCCorner + wC;\n\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n getValue(batch, xR, xC + 3, d)\n );\n\n "+d+"\n }\n\n int xC = xCCorner + "+h+";\n if ("+(1===p)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+d+"\n } else if ("+(2===p)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n initializationValue,\n initializationValue\n );\n\n "+d+"\n } else if ("+(3===p)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n initializationValue\n );\n\n "+d+"\n }\n }\n setOutput("+f+");\n }\n "}}}(),yu=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="sum"===e,u="0.0";i||(u="min"===e?"1.0 / 0.0":"-1.0 / 0.0");var s="min"===e?"min":"max",c=e+"("+e+"("+e+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"sum"===e&&(c="sumValue");var l=4*Math.floor(n/4),f=n%4,h="\n if ("+i+") {\n sumValue += dot(values, ones);\n } else {\n minMaxValue = "+s+"(values, minMaxValue);\n }\n ",p="";o%n>0&&(p="\n if (inIdx < 0 || inIdx >= "+o+") {\n return initializationValue;\n }\n "),this.userCode="\n const float initializationValue = "+u+";\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("+u+");\n float sumValue = 0.0;\n\n for (int i = 0; i < "+l+"; 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 "+h+"\n }\n\n int inIdx = inOffset + "+l+";\n if ("+(1===f)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n "+h+"\n } else if ("+(2===f)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n "+h+"\n } else if ("+(3===f)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n "+h+"\n }\n setOutput("+c+");\n }\n "}}(),bu=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],f=c[0]/l[0],h=c[1]/l[1],p=1/f,d=1/h,v=2*Math.ceil(p)+2,g=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("+f+");\n const float widthScale = float("+h+");\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("+g+");\n\n // Compute bounds for where in dy we will look\n float startRLerp = floor(float(r) * invHeightScale);\n int startDyR = int(startRLerp - float(winHeight / 2));\n\n float startCLerp = floor(float(c) * invWidthScale);\n int startDyC = int(startCLerp - float(winWidth / 2));\n\n // Loop over dy\n for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {\n int dyR = dyROffset + startDyR;\n\n // Guard against the window exceeding the bounds of dy\n if (dyR < 0 || dyR >= "+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 "}}(),xu=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 "}}(),wu=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?[a-1,i-1]:[a,i],c=r?[e-1,n-1]:[e,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 "}}(),Eu=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=qa(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 "}}(),Su=function(){function t(t){this.variableNames=["source"],this.outputShape=t,this.rank=t.length;var e=qa(this.rank),n=function(t){if(1===t)return"sourceLoc";if(2===t)return"sourceLoc.x, sourceLoc.y";if(3===t)return"sourceLoc.x, sourceLoc.y, sourceLoc.z";if(4===t)return"sourceLoc.x, sourceLoc.y, sourceLoc.z, sourceLoc.w";throw Error("Slicing for rank "+t+" is not yet supported")}(this.rank);this.userCode="\n uniform "+e+" start;\n\n void main() {\n "+e+" sourceLoc = start + getOutputCoords();\n setOutput(getSource("+n+"));\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){if(null!=e.startLoc||(e.startLoc=n.getUniformLocationNoThrow(r,"start"),null!=e.startLoc))if(1===e.rank)n.gl.uniform1i(e.startLoc,t[0]);else if(2===e.rank)n.gl.uniform2i(e.startLoc,t[0],t[1]);else if(3===e.rank)n.gl.uniform3i(e.startLoc,t[0],t[1],t[2]);else{if(4!==e.rank)throw Error("Slicing for rank "+e.rank+" is not yet supported");n.gl.uniform4i(e.startLoc,t[0],t[1],t[2],t[3])}}},t}();var Ru=function(){return function(t,e,n){this.variableNames=["x"],this.outputShape=n,this.rank=n.length;var r=qa(this.rank),o="";o=1===this.rank?"coords * strides + begin":n.map(function(t,e){return"coords["+e+"] * strides["+e+"] + begin["+e+"]"}).join(","),this.userCode="\n "+r+" begin = "+r+"("+t+");\n "+r+" strides = "+r+"("+e+");\n\n void main() {\n "+r+" coords = getOutputCoords();\n setOutput(getX("+o+"));\n }\n "}}(),ku=function(){function t(t){this.gpgpu=t,this.numUsedTextures=0,this.numFreeTextures=0,this.freeTextures={},this.logEnabled=!1,this.allocatedTextures=[],this.usedTextureCount={}}return t.prototype.acquireTexture=function(t,e){void 0===e&&(e=ma.FLOAT);var n=Cu(t,e);if(n in this.freeTextures||(this.freeTextures[n]=[]),n in this.usedTextureCount||(this.usedTextureCount[n]=0),this.usedTextureCount[n]++,this.freeTextures[n].length>0)return this.numFreeTextures--,this.numUsedTextures++,this.log(),this.freeTextures[n].shift();this.numUsedTextures++,this.log();var r=this.gpgpu.createMatrixTexture(t[0],t[1]);return this.allocatedTextures.push(r),r},t.prototype.releaseTexture=function(t,e,n){void 0===n&&(n=ma.FLOAT);var r=Cu(e,n);r in this.freeTextures||(this.freeTextures[r]=[]),this.freeTextures[r].push(t),this.numFreeTextures++,this.numUsedTextures--,this.usedTextureCount[r]--,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;null!=this.allocatedTextures&&(this.allocatedTextures.forEach(function(e){t.gpgpu.deleteMatrixTexture(e)}),this.freeTextures=null,this.allocatedTextures=null,this.usedTextureCount=null,this.numUsedTextures=0,this.numFreeTextures=0)},t}();function Cu(t,e){return t[0]+"_"+t[1]+"_"+e}var Tu=function(){return function(t,e){this.variableNames=["A"];for(var n=new Array(t.length),r=0;r4)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"],r=[],o=0;o4)throw Error("Transpose for rank "+e+" is not yet supported");for(var n=["resRC.x","resRC.y","resRC.z","resRC.w"],r=new Array(e),o=0;o0&&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?this.gpgpu.beginQuery():{startMs:performance.now(),endMs:null}},t.prototype.endTimer=function(t){return fa.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?(this.gpgpu.endQuery(),t):(t.endMs=performance.now(),t)},t.prototype.getQueryTime=function(t){return Ou(this,void 0,void 0,function(){var e;return Nu(this,function(n){return fa.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?[2,this.gpgpu.pollQueryTime(t)]:[2,(e=t).endMs-e.startMs]})})},t.prototype.disposeData=function(t){if(this.texData.has(t)){var e=this.texData.get(t),n=e.texture,r=e.texShape,o=e.texType;null!=n&&this.textureManager.releaseTexture(n,r,o),this.texData.delete(t)}},t.prototype.getTexture=function(t){return this.uploadToGPU(t),this.texData.get(t).texture},t.prototype.getTextureData=function(t){return this.uploadToGPU(t),this.texData.get(t)},t.prototype.getGPGPUContext=function(){return this.gpgpu},t.prototype.getCanvas=function(){return this.canvas},t.prototype.slice=function(t,e,n){var r=new Su(n),o=r.getCustomSetupFunc(e);return this.compileAndRun(r,[t],null,o)},t.prototype.stridedSlice=function(t,e,n,r,o,a){var i=le(t.shape,e,n,r,o,a),u=i[0],s=i[1];if(s.some(function(t){return 0===t}))return eo([],s);var c=new Ru(u,r,s);return this.compileAndRun(c,[t])},t.prototype.reverse=function(t,e){var n=new Eu(t.shape,e);return this.compileAndRun(n,[t])},t.prototype.concat=function(t,e){var n=new Sa(t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.neg=function(t){var e=new Au(t.shape,"return -x;");return this.compileAndRun(e,[t])},t.prototype.matMul=function(t,e,n,r){var o=new pu(t.shape,e.shape,n,r);return this.compileAndRun(o,[t,e])},t.prototype.multiply=function(t,e){var n=new wa("return a * b;",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,kt(t.dtype,e.dtype));return this.compileAndRun(n,[t,e],r)},t.prototype.batchNormalization=function(t,e,n,r,o,a){var i=[t,e,n],u=null;null!=a&&(u=a.shape,i.push(a));var s=null;null!=o&&(s=o.shape,i.push(o));var c=new xa(t.shape,e.shape,n.shape,u,s,r);return this.compileAndRun(c,i)},t.prototype.localResponseNormalization4D=function(t,e,n,r,o){var a=new fu(t.shape,e,n,r,o);return this.compileAndRun(a,[t])},t.prototype.tile=function(t,e){var n=new Tu(t.shape,e);return this.compileAndRun(n,[t])},t.prototype.pad=function(t,e,n){var r=new gu(t.shape,e,n);return this.compileAndRun(r,[t])},t.prototype.transpose=function(t,e){var n=new _u(t.shape,e);return this.compileAndRun(n,[t])},t.prototype.gather=function(t,e,n){var r=new Za(t.shape,e.size,n);return this.compileAndRun(r,[t,e])},t.prototype.reduce=function(t,e,n){var r=t.shape[0],o=t.shape[1],a=da(o),i=new yu({windowSize:a,inSize:o,batchSize:r},e),u=i.outputShape,s=u[0],c=u[1],l=this.makeOutputArray([s,c],n);return this.compileAndRun(i,[t],l),1===l.shape[1]?l:this.reduce(l,e,n)},t.prototype.argReduce=function(t,e,n){void 0===n&&(n=null);var r=t.shape[0],o=t.shape[1];null!=n&&(r=n.shape[0],o=n.shape[1]);var a=da(o),i=new ya({windowSize:a,inSize:o,batchSize:r},e,null==n),u=i.outputShape,s=u[0],c=u[1],l=this.makeOutputArray([s,c],"int32"),f=[t];return null!=n&&f.push(n),this.compileAndRun(i,f,l),1===l.shape[1]?l:this.argReduce(t,e,l)},t.prototype.sum=function(t,e){G("sum",e,t.rank);var n=z(t.shape,e),r=n[0],o=p(n[1]),a=t.as2D(-1,o),i=kt(t.dtype,"int32");return this.reduce(a,"sum",i).reshape(r)},t.prototype.argMin=function(t,e){var n=[e];G("argMin",n,t.rank);var r=z(t.shape,n),o=r[0],a=p(r[1]),i=t.as2D(-1,a);return this.argReduce(i,"min").reshape(o)},t.prototype.argMax=function(t,e){var n=[e];G("argMax",n,t.rank);var r=z(t.shape,n),o=r[0],a=p(r[1]),i=t.as2D(-1,a);return this.argReduce(i,"max").reshape(o)},t.prototype.cumsum=function(t,e,n,r){var o=new Xa(t.shape,n,r);return this.compileAndRun(o,[t])},t.prototype.equal=function(t,e){var n=new wa("return float(a == b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.notEqual=function(t,e){var n=new wa("return float(a != b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.less=function(t,e){var n=new wa("return float(a < b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.lessEqual=function(t,e){var n=new wa("return float(a <= b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.greater=function(t,e){var n=new wa("return float(a > b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.greaterEqual=function(t,e){var n=new wa("return float(a >= b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.logicalNot=function(t){var e=new Au(t.shape,"return float(!(x >= 1.0));");return this.compileAndRun(e,[t])},t.prototype.logicalAnd=function(t,e){var n=new wa("return float(a >= 1.0 && b >= 1.0);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.logicalOr=function(t,e){var n=new wa("return float(a >= 1.0 || b >= 1.0);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.where=function(t,e,n,r){var o=new lu(t.rank,e.shape,e.rank),a=this.makeOutputArray(o.outputShape,r);return this.compileAndRun(o,[t,e,n],a)},t.prototype.topKValues=function(t,e){throw new Error("topKValues GPU not yet implemented!")},t.prototype.topKIndices=function(t,e){throw new Error("topKIndices GPU not yet implemented!")},t.prototype.min=function(t,e){G("min",e,t.rank);var n=z(t.shape,e),r=n[0],o=p(n[1]),a=t.as2D(-1,o);return this.reduce(a,"min",a.dtype).reshape(r)},t.prototype.minimum=function(t,e){var n=new wa("\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])},t.prototype.mod=function(t,e){var n=new wa("return mod(a, b);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.max=function(t,e){G("max",e,t.rank);var n=z(t.shape,e),r=n[0],o=p(n[1]),a=t.as2D(-1,o);return this.reduce(a,"max",a.dtype).reshape(r)},t.prototype.maximum=function(t,e){var n=new wa("\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])},t.prototype.squaredDifference=function(t,e){var n=new wa("return (a - b) * (a - b);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.divide=function(t,e){var n,r;"int32"===t.dtype&&"int32"===e.dtype?(n="\n float resultSign = sign(a) * sign(b);\n int ia = round(a);\n int ib = round(b);\n int result = ia / ib;\n int amodb = ia - ib * result;\n\n if (resultSign < 0.0 && amodb != 0) {\n result -= 1;\n }\n return float(result);\n",r="int32"):(n="return a / b;",r="float32");var o=new wa(n,t.shape,e.shape),a=this.makeOutputArray(o.outputShape,r);return this.compileAndRun(o,[t,e],a)},t.prototype.add=function(t,e){var n=new wa("return a + b;",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,kt(t.dtype,e.dtype));return this.compileAndRun(n,[t,e],r)},t.prototype.subtract=function(t,e){var n=new wa("return a - b;",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,kt(t.dtype,e.dtype));return this.compileAndRun(n,[t,e],r)},t.prototype.pow=function(t,e){var n=new wa("\n return (round(mod(b, 2.0)) == 0 || round(mod(b, 2.0)) == 2) ?\n pow(abs(a), b) : sign(a) * pow(abs(a), b);\n",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,kt(t.dtype,e.dtype));return this.compileAndRun(n,[t,e],r)},t.prototype.ceil=function(t){var e=new Au(t.shape,"return ceil(x);");return this.compileAndRun(e,[t])},t.prototype.floor=function(t){var e=new Au(t.shape,"return floor(x);");return this.compileAndRun(e,[t])},t.prototype.sign=function(t){var e=new Au(t.shape,"\n if (isNaN(x)) { return 0.0; }\n return sign(x);\n");return this.compileAndRun(e,[t])},t.prototype.round=function(t){var e=new Au(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])},t.prototype.exp=function(t){var e=new Au(t.shape,"return exp(x);");return this.compileAndRun(e,[t])},t.prototype.expm1=function(t){var e=new Au(t.shape,"return exp(x) - 1.0;");return this.compileAndRun(e,[t])},t.prototype.log=function(t){var e=new Au(t.shape,"return log(x);");return this.compileAndRun(e,[t])},t.prototype.log1p=function(t){var e=new Au(t.shape,"return log(1.0 + x);");return this.compileAndRun(e,[t])},t.prototype.sqrt=function(t){var e=new Au(t.shape,"return sqrt(x);");return this.compileAndRun(e,[t])},t.prototype.rsqrt=function(t){var e=new Au(t.shape,"return inversesqrt(x);");return this.compileAndRun(e,[t])},t.prototype.square=function(t){var e=new Au(t.shape,"return x * x;");return this.compileAndRun(e,[t])},t.prototype.reciprocal=function(t){var e=new Au(t.shape,"return 1.0 / x;");return this.compileAndRun(e,[t])},t.prototype.relu=function(t){var e=new Au(t.shape,Iu);return this.compileAndRun(e,[t])},t.prototype.elu=function(t){var e=new Au(t.shape,"return (x >= 0.0) ? x : (exp(x) - 1.0);");return this.compileAndRun(e,[t])},t.prototype.eluDer=function(t,e){var n=new wa("return (b >= 1.0) ? a : a * (b + 1.0);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.selu=function(t){var e=new Au(t.shape,"\n // Stable and Attracting Fixed Point (0, 1) for Normalized Weights.\n // see: https://arxiv.org/abs/1706.02515\n float scaleAlpha = 1.7580993408473768;\n float scale = 1.0507009873554805;\n return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);\n");return this.compileAndRun(e,[t])},t.prototype.int=function(t){var e=new Au(t.shape,"return float(int(x));"),n=this.makeOutputArray(e.outputShape,"int32");return this.compileAndRun(e,[t],n)},t.prototype.clip=function(t,e,n){var r=new Ea(t.shape,e,n);return this.compileAndRun(r,[t])},t.prototype.abs=function(t){var e=new Au(t.shape,"return abs(x);");return this.compileAndRun(e,[t])},t.prototype.sigmoid=function(t){var e=new Au(t.shape,"return 1.0 / (1.0 + exp(-1.0 * x));");return this.compileAndRun(e,[t])},t.prototype.softplus=function(t){var e=new Au(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])},t.prototype.sin=function(t){var e=new Au(t.shape,"return sin(x);");return this.compileAndRun(e,[t])},t.prototype.cos=function(t){var e=new Au(t.shape,"return cos(x);");return this.compileAndRun(e,[t])},t.prototype.tan=function(t){var e=new Au(t.shape,"return tan(x);");return this.compileAndRun(e,[t])},t.prototype.asin=function(t){var e=new Au(t.shape,"return asin(x);");return this.compileAndRun(e,[t])},t.prototype.acos=function(t){var e=new Au(t.shape,"return acos(x);");return this.compileAndRun(e,[t])},t.prototype.atan=function(t){var e=new Au(t.shape,Mu);return this.compileAndRun(e,[t])},t.prototype.atan2=function(t,e){var n=new wa("\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])},t.prototype.sinh=function(t){var e=new Au(t.shape,"\n float e2x = exp(x);\n return (e2x - 1.0 / e2x) / 2.0;\n");return this.compileAndRun(e,[t])},t.prototype.cosh=function(t){var e=new Au(t.shape,"\n float e2x = exp(-x);\n return (e2x + 1.0 / e2x) / 2.0;\n");return this.compileAndRun(e,[t])},t.prototype.tanh=function(t){var e=new Au(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])},t.prototype.asinh=function(t){var e=new Au(t.shape,"return log(x + sqrt(x * x + 1.0));");return this.compileAndRun(e,[t])},t.prototype.acosh=function(t){var e=new Au(t.shape,"return log(x + sqrt(x * x - 1.0));");return this.compileAndRun(e,[t])},t.prototype.atanh=function(t){var e=new Au(t.shape,"return (log(1.0 + x) - log(1.0 - x)) / 2.0;");return this.compileAndRun(e,[t])},t.prototype.erf=function(t){var e=new Au(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 t = 1.0 / (1.0 + p * x);\n return 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);\n');return this.compileAndRun(e,[t])},t.prototype.step=function(t,e){var n=new Au(t.shape,function(t){return void 0===t&&(t=0),Du+"\n return x > 0.0 ? 1.0 : float("+t+");\n "}(e));return this.compileAndRun(n,[t])},t.prototype.conv2d=function(t,e,n){var r=new Ca(n);return this.compileAndRun(r,[t,e])},t.prototype.conv2dDerInput=function(t,e,n){var r=new ka(n);return this.compileAndRun(r,[t,e])},t.prototype.conv2dDerFilter=function(t,e,n){var r=new Ra(n);return this.compileAndRun(r,[t,e])},t.prototype.depthwiseConv2D=function(t,e,n){var r=new Ya(n);return this.compileAndRun(r,[t,e])},t.prototype.maxPool=function(t,e){var n=new mu(e,"max",!1),r=this.makeOutputArray(n.outputShape,t.dtype);return this.compileAndRun(n,[t],r)},t.prototype.avgPool=function(t,e){var n=new mu(e,"avg",!1),r=this.makeOutputArray(n.outputShape,"float32");return this.compileAndRun(n,[t],r)},t.prototype.maxPoolBackprop=function(t,e,n,r){var o=new mu(r,"max",!0),a=this.compileAndRun(o,[e]),i=new hu(r),u=this.makeOutputArray(i.outputShape,e.dtype),s=this.compileAndRun(i,[t,a],u);return a.dispose(),s},t.prototype.avgPoolBackprop=function(t,e,n){var r=new ba(n),o=this.makeOutputArray(r.outputShape,e.dtype);return this.compileAndRun(r,[t],o)},t.prototype.cast=function(t,e){return va(t,e,this)},t.prototype.reshape=function(t,e){return ga(t,e)},t.prototype.resizeBilinear=function(t,e,n,r){var o=new xu(t.shape,e,n,r);return this.compileAndRun(o,[t])},t.prototype.resizeBilinearBackprop=function(t,e,n){var r=new bu(t,e,n);return this.compileAndRun(r,[t])},t.prototype.resizeNearestNeighbor=function(t,e,n,r){var o=new wu(t.shape,e,n,r);return this.compileAndRun(o,[t])},t.prototype.multinomial=function(t,e,n,r){var o=e?t:Eo(t),a=o.shape[0],i=o.shape[1],u=new du(a,i,n),s=this.makeOutputArray(u.outputShape,"int32"),c=u.getCustomSetupFunc(r);return this.compileAndRun(u,[o],s,c)},t.prototype.oneHot=function(t,e,n,r){var o=new vu(t.size,e,n,r);return this.compileAndRun(o,[t])},t.prototype.makeOutputArray=function(t,e){return Mo.make(t,{},e)},t.prototype.compileAndRun=function(t,e,n,r){var o=this;null==n&&(n=this.makeOutputArray(t.outputShape,e[0].dtype));var a=e.map(function(t){return o.uploadToGPU(t.dataId),{tensor:t,texData:o.texData.get(t.dataId)}});this.uploadToGPU(n.dataId);var i,u={tensor:n,texData:this.texData.get(n.dataId)},s=function(t,e,n){var r="";e.concat(n).forEach(function(t){r+=t.tensor.shape+"_"+t.texData.texShape});var o=t.userCode,a=(!0===t.supportsBroadcasting).toString(),i=t.constructor.name;return i+="_"+a+"_"+r+"_"+o}(t,a,u),c=this.getAndSaveBinary(s,function(){return function(t,e,n,r){for(var o=e.userCode,a=n.map(function(t,n){var r={logicalShape:t.tensor.shape,texShape:t.texData.texShape};return{name:e.variableNames[n],shapeInfo:r}}),i=a.map(function(t){return t.shapeInfo}),u={logicalShape:r.tensor.shape,texShape:r.texData.texShape},s=Ba(a,u,o,!0===e.supportsBroadcasting),c=t.createProgram(s),l={},f=0;f0&&o[o.length-1])&&(6===a[0]||2===a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]h&&(h=g,d=v)}s[l]=d}return i},t.prototype.cumsum=function(t,e,n,r){for(var o=kt(t.dtype,"int32"),a=Fr(t.shape,o),i=a.dataSync(),u=t.dataSync(),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})},t.prototype.greaterEqual=function(t,e){return this.broadcastedBinaryOp(t,e,"bool",function(t,e){return t>=e?1:0})},t.prototype.logicalNot=function(t){for(var e=t.dataSync(),n=new Int32Array(e.length),r=0;r1||1===e.rank?1:e.shape[1],f=0;f=0&&e>=0?n:(n+e)%e})},t.prototype.max=function(t,e){G("max",e,t.rank);for(var n=z(t.shape,e),r=n[0],o=n[1],a=Fr(r,t.dtype),i=p(o),u=a.dataSync(),s=t.dataSync(),c=0;cf&&(f=d)}u[c]=f}return a},t.prototype.maximum=function(t,e){return this.broadcastedBinaryOp(t,e,t.dtype,function(t,e){return Math.max(t,e)})},t.prototype.squaredDifference=function(t,e){return this.broadcastedBinaryOp(t,e,t.dtype,function(t,e){var n=t-e;return n*n})},t.prototype.ceil=function(t){for(var e=t.dataSync(),n=new Float32Array(e.length),r=0;r0?n[r]=1:n[r]=0;return Mo.make(t.shape,{values:n})},t.prototype.round=function(t){for(var e=t.dataSync(),n=new Float32Array(e.length),r=0;r.5?n[r]=Math.ceil(e[r]):n[r]=o%2==0?o:o+1}return Mo.make(t.shape,{values:n})},t.prototype.exp=function(t){for(var e=t.dataSync(),n=new Float32Array(e.length),r=0;r=0?o:Math.exp(o)-1}return Mo.make(t.shape,{values:e})},t.prototype.eluDer=function(t,e){for(var n=new Float32Array(e.size),r=e.dataSync(),o=t.dataSync(),a=0;a=1?o[a]:o[a]*(i+1)}return Mo.make(e.shape,{values:n})},t.prototype.selu=function(t){for(var e=new Float32Array(t.size),n=t.dataSync(),r=0;r=0?1.0507009873554805*o:1.7580993408473768*(Math.exp(o)-1)}return Mo.make(t.shape,{values:e})},t.prototype.clip=function(t,e,n){for(var r=new Float32Array(t.size),o=t.dataSync(),a=0;a-e,i=r[o]0?1:e}return Mo.make(t.shape,{values:n})},t.prototype.conv2d=function(t,e,n){for(var r=n.filterHeight,o=n.filterWidth,a=n.dilationHeight,i=n.dilationWidth,u=n.padInfo.left,s=n.padInfo.top,c=$r(n.outShape,t.dtype),l=0;l=n.inHeight))for(var b=0;b=n.inWidth))for(var w=0;w=n.inHeight))for(var w=0;w=n.inWidth))y+=t.get(f,x,E,h)*e.get(b,w,h,m)}}l.set(y,f,p,v,h*c+m)}return l.toTensor()},t.prototype.tile=function(t,e){for(var n=new Array(t.rank),r=0;rx?x=k:"avg"===n&&(w+=k,E++)}if(isNaN(x))break}u.set("avg"===n?w/E:x,l,h,g,f)}return u.toTensor()},t.prototype.maxPool=function(t,e){return this.pool(t,e,"max")},t.prototype.maxPoolPositions=function(t,e){for(var n=$r(e.outShape,"int32"),r=e.strideHeight,o=e.strideWidth,a=e.filterHeight,i=e.filterWidth,u=e.padInfo.top,s=e.padInfo.left,c=0;cb&&(b=k,x=E*i+R)}n.set(x,c,f,v,l)}return n.toTensor()},t.prototype.maxPoolBackprop=function(t,e,n,r){for(var o=this.maxPoolPositions(e,r),a=r.strideHeight,i=r.strideWidth,u=r.filterHeight,s=r.filterWidth,c=s-1-r.padInfo.left,l=u-1-r.padInfo.top,f=$r(e.shape,"float32"),h=0;h=r.outHeight||Math.floor(x)!==x))for(var w=0;w=r.outWidth||Math.floor(E)!==E)){var S=u*s-1-o.get(h,x,E,p)===b*s+w?1:0;if(0!==S)y+=t.get(h,x,E,p)*S}}}f.set(y,h,d,v,p)}return f.toTensor()},t.prototype.avgPoolBackprop=function(t,e,n){for(var r=n.strideHeight,o=n.strideWidth,a=n.filterHeight,i=n.filterWidth,u=i-1-n.padInfo.left,s=a-1-n.padInfo.top,c=$r(e.shape,"float32"),l=1/(a*i),f=0;f=n.outHeight||Math.floor(b)!==b))for(var x=0;x=n.outWidth||Math.floor(w)!==w))m+=t.get(f,b,w,h)}}c.set(m*l,f,p,d,h)}return c.toTensor()},t.prototype.cast=function(t,e){return va(t,e,this)},t.prototype.reshape=function(t,e){return ga(t,e)},t.prototype.avgPool=function(t,e){return this.pool(t,e,"avg").toFloat()},t.prototype.resizeBilinear=function(t,e,n,r){for(var o=t.shape,a=o[0],i=o[1],u=o[2],s=o[3],c=$r([a,e,n,s],t.dtype),l=[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],h=0;h1?a-1:a,n&&l>1?i-1:i],p=[n&&c>1?c-1:c,n&&l>1?l-1:l],d=h[0]/p[0],v=h[1]/p[1],g=0;g=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},zu=function(){function t(){}return t.nextFrame=function(){return new Promise(function(t){return requestAnimationFrame(function(){return t()})})},Uu([function(){for(var t=[],e=0;e0&&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&&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,"scheme must not be an empty string.");var r=t.getInstance();s(null==r.managers[e],"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 Zu(t){if(-1===t.indexOf(Yu))throw new Error("The url string provided does not contain a scheme. Supported schemes are: "+Ju.getSchemes().join(","));return{scheme:t.split(Yu)[0],path:t.split(Yu)[1]}}function $u(t,e,n){return void 0===n&&(n=!1),Ku(this,void 0,void 0,function(){var r,o,a,i,u,c,l,f,h;return Qu(this,function(p){switch(p.label){case 0:return s(t!==e,"Old path and new path are the same: '"+t+"'"),s((r=Xu.getLoadHandlers(t)).length>0,"Copying failed because no load handler is found for source URL "+t+"."),s(r.length<2,"Copying failed because more than one ("+r.length+") load handlers for source URL "+t+"."),o=r[0],s((a=Xu.getSaveHandlers(e)).length>0,"Copying failed because no save handler is found for destination URL "+e+"."),s(a.length<2,"Copying failed because more than one ("+r.length+") save handlers for destination URL "+e+"."),i=a[0],u=Zu(t).scheme,c=Zu(t).path,l=u===Zu(t).scheme,[4,o.load()];case 1:return f=p.sent(),n&&l?[4,Ju.getManager(u).removeModel(c)]:[3,3];case 2:p.sent(),p.label=3;case 3:return[4,i.save(f)];case 4:return h=p.sent(),!n||l?[3,6]:[4,Ju.getManager(u).removeModel(c)];case 5:p.sent(),p.label=6;case 6:return[2,h.modelArtifactsInfo]}})})}var ts=function(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())})},es=function(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=r[2&a[0]?"return":a[0]?"throw":"next"])&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(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&&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&&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&&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,"URL path for browserHTTPRequest must not be null, undefined or empty."),this.path=t,null!=e&&null!=e.body)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=e||{}}return t.prototype.save=function(t){return As(this,void 0,void 0,function(){var e,n,r,o;return Ds(this,function(a){switch(a.label){case 0:if(t.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");return(e=Object.assign({method:this.DEFAULT_METHOD},this.requestInit)).body=new FormData,n=[{paths:["./model.weights.bin"],weights:t.weightSpecs}],r={modelTopology:t.modelTopology,weightsManifest:n},e.body.append("model.json",new Blob([JSON.stringify(r)],{type:"application/json"}),"model.json"),null!=t.weightData&&e.body.append("model.weights.bin",new Blob([t.weightData],{type:"application/octet-stream"}),"model.weights.bin"),[4,fetch(this.path,e)];case 1:if(200===(o=a.sent()).status)return[2,{modelArtifactsInfo:Hu(t),responses:[o]}];throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status "+o.status+".")}})})},t.URL_SCHEMES=["http://","https://"],t}();function Ms(t,e){return new Is(t,e)}Xu.registerSaveRouter(function(t){if(fa.get("IS_BROWSER")){for(var e=0,n=Is.URL_SCHEMES;e0&&o[o.length-1])&&(6===a[0]||2===a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]n)}var Hs=Object.freeze({WEBGL_ENVS:{BACKEND:"test-webgl"},CPU_ENVS:{BACKEND:"test-cpu"},ALL_ENVS:{},TEST_EPSILON:Gs,expectArraysClose:qs,expectPromiseToFail:function(t,e){t().then(function(){return e.fail()},function(){return e()})},expectArraysEqual:function(t,e){return qs(t,e,0)},expectNumbersClose:function(t,e,n){if(void 0===n&&(n=Gs),!js(t,e,n))throw new Error("Numbers differ: actual === "+t+", expected === "+e)},expectValuesInRange:function(t,e,n){var r;r=t instanceof Mo?t.dataSync():t;for(var o=0;on)throw new Error("Value out of range:"+r[o]+" low: "+e+", high: "+n)}}),Xs=Object.freeze({gpgpu_util:ru,webgl_util:Oi,MathBackendWebGL:Lu,GPGPUContext:iu}),Ks=function(){var 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])};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),Qs=function(t,e,n,r){var o,a=arguments.length,i=a<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(t,e,n,r);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},Ys=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return Ks(e,t),e.prototype.minimize=function(t,e,n){void 0===e&&(e=!1);var r=this.computeGradients(t,n),o=r.value,a=r.grads;return this.applyGradients(a),Object.keys(a).forEach(function(t){return a[t].dispose()}),e?o:(o.dispose(),null)},e.prototype.computeGradients=function(t,e){return Xo(t,e)},Qs([function(){for(var t=[],e=0;e=0;u--)(o=t[u])&&(i=(a<3?o(i):a>3?o(e,n,i):o(e,n))||i);return a>3&&i&&Object.defineProperty(e,n,i),i},hc=function(){function t(){}return t.sgd=function(t){return new ic(t)},t.momentum=function(t,e,n){return void 0===n&&(n=!1),new sc(t,e,n)},t.rmsprop=function(t,e,n,r,o){return void 0===e&&(e=.9),void 0===n&&(n=0),void 0===r&&(r=1e-8),void 0===o&&(o=!1),new lc(t,e,n,r,o)},t.adam=function(t,e,n,r){return void 0===t&&(t=.001),void 0===e&&(e=.9),void 0===n&&(n=.999),void 0===r&&(r=1e-8),new nc(t,e,n,r)},t.adadelta=function(t,e,n){return void 0===t&&(t=.001),void 0===e&&(e=.95),void 0===n&&(n=1e-8),new Zs(t,e,n)},t.adamax=function(t,e,n,r,o){return void 0===t&&(t=.002),void 0===e&&(e=.9),void 0===n&&(n=.999),void 0===r&&(r=1e-8),void 0===o&&(o=0),new oc(t,e,n,r,o)},t.adagrad=function(t,e){return void 0===e&&(e=.1),new tc(t,e)},fc([function(){for(var t=[],e=0;e0&&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&&e>0?{width:t,height:e}:null},enumerable:!0,configurable:!0}),t}();function Lc(t){return Uo(function(){if(t instanceof Mo){var e=t.shape.length;if(3!==e&&4!==e)throw new Error("input tensor must be of rank 3 or 4");return(3===e?t.expandDims(0):t).toFloat()}var n=t instanceof Nc?t:new Nc(t);return _e(n.canvases.map(function(t){return Nr(t).expandDims(0).toFloat()}))})}function Pc(t,e){return void 0===e&&(e=!1),Uo(function(){var n=t.shape.slice(1),r=n[0],o=n[1];if(r===o)return t;var a=Math.abs(r-o),i=Math.floor(a*(e?.5:1)),u=r>o?2:1,s=function(e){void 0===e&&(e=!1);var n=t.shape.slice();return n[u]=i+(e?1:0),n},c=(e?[to(s(!Sc(a)),0)]:[]).concat([t,to(s(),0)]);return _e(c,u)})}function Bc(t){var e=t;return{extractWeights:function(t){var n=e.slice(0,t);return e=e.slice(t),n},getRemainingWeights:function(){return e}}}function Fc(t){function e(e,n,r){return{filters:io(t(e*n*r*r),[r,r,e,n]),bias:ro(t(n))}}function n(t,n,r){var o=e(t,n,r);return{filters:o.filters,batch_norm_offset:o.bias}}function r(e,r){var o;return{depthwise_conv_params:{filters:io(t(9*(o=e)),[3,3,o,1]),batch_norm_scale:ro(t(o)),batch_norm_offset:ro(t(o)),batch_norm_mean:ro(t(o)),batch_norm_variance:ro(t(o))},pointwise_conv_params:n(e,r,1)}}return{extractMobilenetV1Params:function(){return{conv_0_params:n(3,32,3),conv_pair_params:[[32,64],[64,128],[128,128],[128,256],[256,256],[256,512],[512,512],[512,512],[512,512],[512,512],[512,512],[512,1024],[1024,1024]].map(function(t){return r(t[0],t[1])})}},extractPredictionLayerParams:function(){return{conv_0_params:n(1024,256,1),conv_1_params:n(256,512,3),conv_2_params:n(512,128,1),conv_3_params:n(128,256,3),conv_4_params:n(256,128,1),conv_5_params:n(128,256,3),conv_6_params:n(256,64,1),conv_7_params:n(64,128,3),box_predictor_0_params:{box_encoding_predictor_params:e(512,12,1),class_predictor_params:e(512,9,1)},box_predictor_1_params:{box_encoding_predictor_params:e(1024,24,1),class_predictor_params:e(1024,18,1)},box_predictor_2_params:{box_encoding_predictor_params:e(512,24,1),class_predictor_params:e(512,18,1)},box_predictor_3_params:{box_encoding_predictor_params:e(256,24,1),class_predictor_params:e(256,18,1)},box_predictor_4_params:{box_encoding_predictor_params:e(256,24,1),class_predictor_params:e(256,18,1)},box_predictor_5_params:{box_encoding_predictor_params:e(128,24,1),class_predictor_params:e(128,18,1)}}}}}var Uc=function(){function t(t,e,n,r){this.x=t,this.y=e,this.width=n,this.height=r}return t.prototype.floor=function(){return new t(Math.floor(this.x),Math.floor(this.y),Math.floor(this.width),Math.floor(this.height))},t}(),zc=function(){function t(t,e,n){var r=n.width,o=n.height;this._imageWidth=r,this._imageHeight=o,this._score=t,this._box=new Uc(e.x*r,e.y*o,e.width*r,e.height*o)}return t.prototype.getScore=function(){return this._score},t.prototype.getBox=function(){return this._box},t.prototype.getImageWidth=function(){return this._imageWidth},t.prototype.getImageHeight=function(){return this._imageHeight},t.prototype.getRelativeBox=function(){return new Uc(this._box.x/this._imageWidth,this._box.y/this._imageHeight,this._box.width/this._imageWidth,this._box.height/this._imageHeight)},t.prototype.forSize=function(e,n){return new t(this._score,this.getRelativeBox(),{width:e,height:n})},t}();function Wc(t,e,n){return Uo(function(){var r=Ne(t,e.filters,n,"same");return r=hr(r,e.batch_norm_offset),Bn(r,0,6)})}var Vc=.0010000000474974513;function Gc(t,e){return Uo(function(){var n=null,r=Wc(t,e.conv_0_params,[2,2]);if(e.conv_pair_params.forEach(function(t,e){var o,a,i,u=e+1,s=function(t){return[2,4,6,12].some(function(e){return e===t})?[2,2]:[1,1]}(u);o=r,a=t.depthwise_conv_params,i=s,r=Wc(r=Uo(function(){var t=Pe(o,a.filters,i,"same");return t=Re(t,a.batch_norm_mean,a.batch_norm_variance,Vc,a.batch_norm_scale,a.batch_norm_offset),Bn(t,0,6)}),t.pointwise_conv_params,[1,1]),11===u&&(n=r)}),null===n)throw new Error("mobileNetV1 - output of conv layer 11 is null");return{out:r,conv11:n}})}function qc(t,e,n){var r=Math.min(t.get(e,0),t.get(e,2)),o=Math.min(t.get(e,1),t.get(e,3)),a=Math.max(t.get(e,0),t.get(e,2)),i=Math.max(t.get(e,1),t.get(e,3)),u=Math.min(t.get(n,0),t.get(n,2)),s=Math.min(t.get(n,1),t.get(n,3)),c=Math.max(t.get(n,0),t.get(n,2)),l=Math.max(t.get(n,1),t.get(n,3)),f=(a-r)*(i-o),h=(c-u)*(l-s);if(f<=0||h<=0)return 0;var p=Math.max(r,u),d=Math.max(o,s),v=Math.min(a,c),g=Math.min(i,l),m=Math.max(v-p,0)*Math.max(g-d,0);return m/(f+h-m)}function jc(t,e){var n=function(t){var e=lo(qe(t,[1,0])),n=[Tr(e[2],e[0]),Tr(e[3],e[1])];return{sizes:n,centers:[hr(e[0],vr(n[0],no(2))),hr(e[1],vr(n[1],no(2)))]}}(t),r=n.sizes,o=n.centers,a=lo(qe(e,[1,0])),i=vr(Sr(Wn(vr(a[2],no(5))),r[0]),no(2)),u=hr(Sr(vr(a[0],no(10)),r[0]),o[0]),s=vr(Sr(Wn(vr(a[3],no(5))),r[1]),no(2)),c=hr(Sr(vr(a[1],no(10)),r[1]),o[1]);return qe(co([Tr(u,i),Tr(c,s),hr(u,i),hr(c,s)]),[1,0])}function Hc(t,e,n,r){return void 0===n&&(n="same"),void 0===r&&(r=!1),Uo(function(){var o=hr(Ne(t,e.filters,[1,1],n),e.bias);return r?Jn(o):o})}function Xc(t,e){return Uo(function(){var n=t.shape[0];return{boxPredictionEncoding:Hr(Hc(t,e.box_encoding_predictor_params),[n,-1,1,4]),classPrediction:Hr(Hc(t,e.class_predictor_params),[n,-1,3])}})}var Kc=[512,512],Qc=no(.007843137718737125),Yc=no(1);var Jc=function(){function t(t,e){this.x=t,this.y=e}return t.prototype.add=function(e){return new t(this.x+e.x,this.y+e.y)},t.prototype.sub=function(e){return new t(this.x-e.x,this.y-e.y)},t.prototype.mul=function(e){return new t(this.x*e.x,this.y*e.y)},t.prototype.div=function(e){return new t(this.x/e.x,this.y/e.y)},t.prototype.abs=function(){return new t(Math.abs(this.x),Math.abs(this.y))},t.prototype.magnitude=function(){return Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2))},t.prototype.floor=function(){return new t(Math.floor(this.x),Math.floor(this.y))},t}();function Zc(t){var e=Bc(t),n=e.extractWeights,r=e.getRemainingWeights,o=function(t){return function(e,n,r){return{filters:io(t(e*n*r*r),[r,r,e,n]),bias:ro(t(n))}}}(n);function a(t,e){return{weights:oo(n(t*e),[t,e]),bias:ro(n(e))}}var i=o(3,32,3),u=o(32,64,3),s=o(64,64,3),c=o(64,64,3),l=o(64,64,3),f=o(64,128,3),h=o(128,128,3),p=o(128,256,3),d=a(6400,1024),v=a(1024,136);if(0!==r().length)throw new Error("weights remaing after extract: "+r().length);return{conv0_params:i,conv1_params:u,conv2_params:s,conv3_params:c,conv4_params:l,conv5_params:f,conv6_params:h,conv7_params:p,fc0_params:d,fc1_params:v}}function $c(t){return t.reduce(function(t,e){return t.add(e)},new Jc(0,0)).div(new Jc(t.length,t.length))}var tl=function(){function t(t,e,n){void 0===n&&(n=new Jc(0,0));var r=e.width,o=e.height;this._imageWidth=r,this._imageHeight=o,this._shift=n,this._faceLandmarks=t.map(function(t){return t.mul(new Jc(r,o)).add(n)})}return t.prototype.getShift=function(){return new Jc(this._shift.x,this._shift.y)},t.prototype.getImageWidth=function(){return this._imageWidth},t.prototype.getImageHeight=function(){return this._imageHeight},t.prototype.getPositions=function(){return this._faceLandmarks},t.prototype.getRelativePositions=function(){var t=this;return this._faceLandmarks.map(function(e){return e.sub(t._shift).div(new Jc(t._imageWidth,t._imageHeight))})},t.prototype.getJawOutline=function(){return this._faceLandmarks.slice(0,17)},t.prototype.getLeftEyeBrow=function(){return this._faceLandmarks.slice(17,22)},t.prototype.getRightEyeBrow=function(){return this._faceLandmarks.slice(22,27)},t.prototype.getNose=function(){return this._faceLandmarks.slice(27,36)},t.prototype.getLeftEye=function(){return this._faceLandmarks.slice(36,42)},t.prototype.getRightEye=function(){return this._faceLandmarks.slice(42,48)},t.prototype.getMouth=function(){return this._faceLandmarks.slice(48,68)},t.prototype.forSize=function(e,n){return new t(this.getRelativePositions(),{width:e,height:n})},t.prototype.shift=function(e,n){return new t(this.getRelativePositions(),{width:this._imageWidth,height:this._imageHeight},new Jc(e,n))},t.prototype.align=function(t){if(t){var e=t instanceof zc?t.getBox().floor():t;return this.shift(e.x,e.y).align()}var n=[this.getLeftEye(),this.getRightEye(),this.getMouth()].map($c),r=n[0],o=n[1],a=n[2],i=function(t){return a.sub(t).magnitude()},u=(i(r)+i(o))/2,s=Math.floor(u/.45),c=$c(n),l=Math.floor(Math.max(0,c.x-.5*s)),f=Math.floor(Math.max(0,c.y-.43*s));return new Uc(l,f,s,s)},t}();function el(t,e){return Uo(function(){return hr(Fe(t,e.weights),e.bias)})}function nl(t,e){return Hc(t,e,"valid",!0)}function rl(t,e){return void 0===e&&(e=[2,2]),Ge(t,[2,2],e,"valid")}function ol(t,e,n,r,o){void 0===o&&(o="same");var a=e.conv,i=a.filters,u=a.bias,s=Ne(t,i,n,o);return s=function(t,e){return hr(Sr(t,e.weights),e.biases)}(s=hr(s,u),e.scale),r?Jn(s):s}function al(t,e){return ol(t,e,[1,1],!1)}function il(t,e){return ol(t,e,[2,2],!0,"valid")}function ul(t){function e(e,n,r){var o;return{conv:{filters:function(e,n,r){var o=t(e),a=o.length/(n*r*r);if(Ec(a))throw new Error("depth has to be an integer: "+a+", weights.length: "+o.length+", numFilters: "+n+", filterSize: "+r);return qe(io(o,[n,a,r,r]),[2,3,1,0])}(e,n,r),bias:ro(t(n))},scale:{weights:ro(t(o=n)),biases:ro(t(o))}}}return{extractConvLayerParams:e,extractResidualLayerParams:function(t,n,r,o){return void 0===o&&(o=!1),{conv1:e((o?.5:1)*t,n,r),conv2:e(t,n,r)}}}}function sl(t,e){var n=function(t,e){return ol(t,e,[1,1],!0)}(t,e.conv1);return n=al(n,e.conv2),n=hr(n,t),n=Jn(n)}function cl(t,e){var n=il(t,e.conv1);n=al(n,e.conv2);var r=Ve(t,2,2,"valid"),o=Fr(r.shape),a=r.shape[3]!==n.shape[3];if(r.shape[1]!==n.shape[1]||r.shape[2]!==n.shape[2]){var i=n.shape.slice();i[1]=1;var u=Fr(i),s=(n=_e([n,u],1)).shape.slice();s[2]=1;var c=Fr(s);n=_e([n,c],2)}return r=a?_e([r,o],3):r,n=hr(r,n),n=Jn(n)}t.euclideanDistance=function(t,e){if(t.length!==e.length)throw new Error("euclideanDistance: arr1.length !== arr2.length");var n=Array.from(t),r=Array.from(e);return Math.sqrt(n.map(function(t,e){return t-r[e]}).reduce(function(t,e){return t+Math.pow(e,2)},0))},t.faceDetectionNet=function(t){var e=function(t){var e=Bc(t),n=e.extractWeights,r=e.getRemainingWeights,o=Fc(n),a=o.extractMobilenetV1Params,i=o.extractPredictionLayerParams,u=a(),s=i(),c={extra_dim:ao(n(20472),[1,5118,4])};if(0!==r().length)throw new Error("weights remaing after extract: "+r().length);return{mobilenetv1_params:u,prediction_layer_params:s,output_layer_params:c}}(t);function n(t){return Uo(function(){var n,r=Gc((n=t,Uo(function(){var t=Co.resizeBilinear(n,Kc,!1);return Tr(Sr(t,Qc),Yc)})),e.mobilenetv1_params),o=function(t,e,n){return Uo(function(){var r=Wc(Wc(t,n.conv_0_params,[1,1]),n.conv_1_params,[2,2]),o=Wc(Wc(r,n.conv_2_params,[1,1]),n.conv_3_params,[2,2]),a=Wc(Wc(o,n.conv_4_params,[1,1]),n.conv_5_params,[2,2]),i=Wc(Wc(a,n.conv_6_params,[1,1]),n.conv_7_params,[2,2]),u=Xc(e,n.box_predictor_0_params),s=Xc(t,n.box_predictor_1_params),c=Xc(r,n.box_predictor_2_params),l=Xc(o,n.box_predictor_3_params),f=Xc(a,n.box_predictor_4_params),h=Xc(i,n.box_predictor_5_params);return{boxPredictions:_e([u.boxPredictionEncoding,s.boxPredictionEncoding,c.boxPredictionEncoding,l.boxPredictionEncoding,f.boxPredictionEncoding,h.boxPredictionEncoding],1),classPredictions:_e([u.classPrediction,s.classPrediction,c.classPrediction,l.classPrediction,f.classPrediction,h.classPrediction],1)}})}(r.out,r.conv11,e.prediction_layer_params);return function(t,e,n){return Uo(function(){var r=t.shape[0],o=jc(Hr(Kr(n.extra_dim,[r,1,1]),[-1,4]),Hr(t,[-1,4]));o=Hr(o,[r,o.shape[0]/r,4]);var a=er(Ye(e,[0,0,1],[-1,-1,-1])),i=Ye(a,[0,0,0],[-1,-1,1]);return i=Hr(i,[r,i.shape[1]]),{boxes:lo(o),scores:lo(i)}})}(o.boxPredictions,o.classPredictions,e.output_layer_params)})}return{forward:function(t){return Uo(function(){return n(Pc(Lc(t)))})},locateFaces:function(t,e,r){return void 0===e&&(e=.8),void 0===r&&(r=100),xc(this,void 0,void 0,function(){var o,a,i,u,s,c,l,f,h,p,d,v,g,m;return wc(this,function(y){switch(y.label){case 0:for(o=1,a=1,u=Uo(function(){var e=Lc(t),r=e.shape.slice(1),u=r[0],s=r[1];return i={width:s,height:u},e=Pc(e),o=e.shape[1]/u,a=e.shape[2]/s,n(e)}),s=u.boxes,c=u.scores,l=s[0],f=c[0],h=1;ho}).sort(function(t,e){return e.score-t.score}).forEach(function(e){if(!(u.length>=i)){for(var n=e.score,a=u.length-1;a>=0;--a){var s=qc(t,e.boxIndex,u[a]);if(0!==s&&(e.score*=s<=r?1:0,e.score<=o))break}n===e.score&&u.push(e.boxIndex)}}),u}(l,p,r,.5,e),m=g.map(function(t){var e=[Math.max(0,l.get(t,0)),Math.min(1,l.get(t,2))].map(function(t){return t*o}),n=e[0],r=e[1],u=[Math.max(0,l.get(t,1)),Math.min(1,l.get(t,3))].map(function(t){return t*a}),s=u[0],c=u[1];return new zc(p[t],new Uc(s,n,c-s,r-n),i)}),l.dispose(),f.dispose(),[2,m]}})})}}},t.faceLandmarkNet=function(t){var e=Zc(t);return{detectLandmarks:function(t){return xc(this,void 0,void 0,function(){var n,r,o,a,i,u,s;return wc(this,function(c){switch(c.label){case 0:return r=Uo(function(){var r=Lc(t),o=r.shape.slice(1),a=o[0],i=o[1];n={width:i,height:a},128===r.shape[1]&&128===r.shape[2]||(r=Co.resizeBilinear(r,[128,128]));var u=nl(r,e.conv0_params);return u=nl(u=rl(u=nl(u=nl(u=rl(u=nl(u=nl(u=rl(u=nl(u=nl(u=rl(u),e.conv1_params),e.conv2_params)),e.conv3_params),e.conv4_params)),e.conv5_params),e.conv6_params),[1,1]),e.conv7_params),el(Jn(el(u.as2D(u.shape[0],-1),e.fc0_params)),e.fc1_params)}),i=(a=Array).from,[4,r.data()];case 1:return o=i.apply(a,[c.sent()]),r.dispose(),u=o.filter(function(t,e){return(e-1)%2}),s=o.filter(function(t,e){return e%2}),[2,new tl(Array(68).fill(0).map(function(t,e){return new Jc(u[e],s[e])}),n)]}})})}}},t.faceRecognitionNet=function(t){var e=this,n=function(t){var e=Bc(t),n=e.extractWeights,r=e.getRemainingWeights,o=ul(n),a=o.extractConvLayerParams,i=o.extractResidualLayerParams,u=a(4704,32,7),s=i(9216,32,3),c=i(9216,32,3),l=i(9216,32,3),f=i(36864,64,3,!0),h=i(36864,64,3),p=i(36864,64,3),d=i(36864,64,3),v=i(147456,128,3,!0),g=i(147456,128,3),m=i(147456,128,3),y=i(589824,256,3,!0),b=i(589824,256,3),x=i(589824,256,3),w=i(589824,256,3),E=qe(oo(n(32768),[128,256]),[1,0]);if(0!==r().length)throw new Error("weights remaing after extract: "+r().length);return{conv32_down:u,conv32_1:s,conv32_2:c,conv32_3:l,conv64_down:f,conv64_1:h,conv64_2:p,conv64_3:d,conv128_down:v,conv128_1:g,conv128_2:m,conv256_down:y,conv256_1:b,conv256_2:x,conv256_down_out:w,fc:E}}(t);function r(t){return Uo(function(){var e=Pc(Lc(t),!0);150===e.shape[1]&&150===e.shape[2]||(e=Co.resizeBilinear(e,[150,150]));var r=il(e=function(t){return Uo(function(){var e=to([1,150,150,1],122.782),n=to([1,150,150,1],117.001),r=to([1,150,150,1],104.298),o=_e([e,n,r],3);return vr(Tr(t,o),no(256))})}(e),n.conv32_down),o=(r=cl(r=sl(r=sl(r=cl(r=sl(r=sl(r=cl(r=sl(r=sl(r=sl(r=cl(r=sl(r=sl(r=sl(r=Ge(r,3,2,"valid"),n.conv32_1),n.conv32_2),n.conv32_3),n.conv64_down),n.conv64_1),n.conv64_2),n.conv64_3),n.conv128_down),n.conv128_1),n.conv128_2),n.conv256_down),n.conv256_1),n.conv256_2),n.conv256_down_out)).mean([1,2]);return Fe(o,n.fc)})}return{computeFaceDescriptor:function(t){return xc(e,void 0,void 0,function(){var e,n;return wc(this,function(o){switch(o.label){case 0:return[4,(e=r(t)).data()];case 1:return n=o.sent(),e.dispose(),[2,n]}})})},computeFaceDescriptorSync:function(t){var e=r(t),n=e.dataSync();return e.dispose(),n},forward:r}},t.NetInput=Nc,t.tf=bc,t.padToSquare=Pc,t.extractFaces=function(t,e){var n=Cc(t);return e.map(function(e){return e instanceof zc?e.forSize(t.width,t.height).getBox().floor():e}).map(function(t){var e=t.x,r=t.y,o=t.width,a=t.height,i=Tc({width:o,height:a});return Cc(i).putImageData(n.getImageData(e,r,o,a),0,0),i})},t.extractFaceTensors=function(t,e){return Uo(function(){var n=Lc(t),r=n.shape,o=(r[0],r[1]),a=r[2],i=r[3];return e.map(function(t){return t instanceof zc?t.forSize(a,o).getBox().floor():t}).map(function(t){var e=t.x,r=t.y,o=t.width,a=t.height;return Ye(n,[0,r,e,0],[1,a,o,i])})})},t.isFloat=Ec,t.isEven=Sc,t.round=Rc,t.getElement=kc,t.getContext2dOrThrow=Cc,t.createCanvas=Tc,t.createCanvasFromMedia=_c,t.getMediaDimensions=Ac,t.bufferToImage=function(t){return new Promise(function(e,n){if(!(t instanceof Blob))return n("bufferToImage - expected buf to be of type: Blob");var r=new FileReader;r.onload=function(){var t=new Image;t.onload=function(){return e(t)},t.onerror=n,t.src=r.result},r.onerror=n,r.readAsDataURL(t)})},t.imageTensorToCanvas=function(t,e){return xc(this,void 0,void 0,function(){var n,r,o,a,i;return wc(this,function(u){switch(u.label){case 0:return n=e||document.createElement("canvas"),(r=t.shape)[0],o=r[1],a=r[2],i=r[3],[4,Lr(t.as3D(o,a,i).toInt(),n)];case 1:return u.sent(),[2,n]}})})},t.getDefaultDrawOptions=Dc,t.drawBox=Ic,t.drawText=Mc,t.drawDetection=function(t,e,n){var r=kc(t);if(!(r instanceof HTMLCanvasElement))throw new Error("drawBox - expected canvas to be of type: HTMLCanvasElement");(Array.isArray(e)?e:[e]).forEach(function(t){var e=t.getBox(),o=e.x,a=e.y,i=e.width,u=e.height,s=Object.assign({color:"blue",lineWidth:2,fontSize:20,fontStyle:"Georgia"},n||{}),c=Object.assign({withScore:!0},n||{}).withScore,l=Cc(r);Ic(l,o,a,i,u,s),c&&Mc(l,o,a,""+Rc(t.getScore()),s)})},t.drawLandmarks=function(t,e,n){var r=kc(t);if(!(r instanceof HTMLCanvasElement))throw new Error("drawLandmarks - expected canvas to be of type: HTMLCanvasElement");var o=Object.assign({color:"blue",lineWidth:2,fontSize:20,fontStyle:"Georgia"},n||{}),a=Object.assign({drawLines:!1},n||{}).drawLines,i=Cc(r),u=o.lineWidth,s=o.color;if(a)return i.strokeStyle=s,i.lineWidth=u,Oc(i,e.getJawOutline()),Oc(i,e.getLeftEyeBrow()),Oc(i,e.getRightEyeBrow()),Oc(i,e.getNose()),Oc(i,e.getLeftEye(),!0),Oc(i,e.getRightEye(),!0),void Oc(i,e.getMouth(),!0);var c=u/2;i.fillStyle=s,e.getPositions().forEach(function(t){return i.fillRect(t.x-c,t.y-c,u,u)})},Object.defineProperty(t,"__esModule",{value:!0})});
+!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(t.faceapi=t.faceapi||{})}(this,function(t){"use strict";var n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var e in n)n.hasOwnProperty(e)&&(t[e]=n[e])};function e(t,e){function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}function r(t,n,e,r){var o,i=arguments.length,a=i<3?n:null===r?r=Object.getOwnPropertyDescriptor(n,e):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,n,e,r);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(a=(i<3?o(a):i>3?o(n,e,a):o(n,e))||a);return i>3&&a&&Object.defineProperty(n,e,a),a}function o(t,n,e,r){return new(e||(e=Promise))(function(o,i){function a(t){try{s(r.next(t))}catch(t){i(t)}}function u(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){t.done?o(t.value):new e(function(n){n(t.value)}).then(a,u)}s((r=r.apply(t,n||[])).next())})}function i(t,n){var e,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;a;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]0;)r=Math.random()*n|0,e=t[--n],t[n]=t[r],t[r]=e}function c(t,n,e){return Math.max(t,Math.min(n,e))}function f(t,n){return Math.random()*(n-t)+t}function h(t,n){if(!t)throw new Error(n)}function p(t,n,e){void 0===e&&(e=""),h(y(t,n),e+" Shapes "+t+" and "+n+" must match")}function d(t,n){h(t.dtype===n.dtype," The dtypes of the first("+t.dtype+") and second("+n.dtype+") input must match")}function v(t,n){if(void 0===n&&(n=[]),Array.isArray(t))for(var e=0;e1;--n)if(t%n==0)return[n,t/n];return[1,t]}function E(t,n){return n<=t.length?t:t+" ".repeat(n-t.length)}function S(t,n,e){return void 0===n&&(n=function(t){return 0}),new Promise(function(r,o){var i=0,a=function(){if(t())r();else{var u=n(++i);null!=e&&i>=e?o():setTimeout(a,u)}};a()})}function R(t){var n={};return t.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,function(t){for(var e=[],r=1;r0)e*=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(n>0&&n!==e)throw Error("Size("+n+") must match the product of shape "+t);return t}if(n%e!=0)throw Error("The implicit shape can't be a fractional number. Got "+n+" / "+e);var i=t.slice();return i[r]=n/e,i}function _(t,n){for(var e=[],r=[],o=0,i=0;i1)throw new Error("Can't squeeze axis "+i+" since its dim '"+t[i]+"' is not 1");(null==n[o]||n[o]>i)&&1===t[i]&&(e.push(t[i]),r.push(i)),n[o]<=i&&o++}t[i]>1&&(e.push(t[i]),r.push(i))}return{newShape:e,keptDims:r}}function k(t,n){var e=null;if(null==t||"float32"===t)e=new Float32Array(n);else if("int32"===t)e=new Int32Array(n);else{if("bool"!==t)throw new Error("Unknown data type "+t);e=new Uint8Array(n)}return e}function T(t,n){for(var e=0;e1)for(var i=0;iP){var s=Array.from(n.subarray(0,U)),l=Array.from(n.subarray(a-U,a));return["["+s.map(function(t,n){return V(t,o[n])}).join(", ")+", ..., "+l.map(function(t,n){return V(t,o[a-U+n])}).join(", ")+"]"]}return["["+Array.from(n).map(function(t,n){return V(t,o[n])}).join(", ")+"]"]}var c=e.slice(1),f=r.slice(1),h=r[0],p=[];if(a>P){for(var d=0;d=-e&&t=1,"Pass at least one tensor to concat"),s({tensors:t},"concat");var e=t[0];if(1===t.length)return e;for(var r=j(n,e.shape),o=1;o=0&&e>>0,n=(r*=n)>>>0,n+=4294967296*(r-=n)}return 2.3283064365386963e-10*(n>>>0)});e.next=function(){var t=2091639*e.s0+2.3283064365386963e-10*e.c;return e.s0=e.s1,e.s1=e.s2,e.s2=t-(e.c=0|t)},e.c=1,e.s0=r(" "),e.s1=r(" "),e.s2=r(" "),e.s0-=r(t),e.s0<0&&(e.s0+=1),e.s1-=r(t),e.s1<0&&(e.s1+=1),e.s2-=r(t),e.s2<0&&(e.s2+=1),r=null}(t),o=n&&n.state,i=e.next;return i.int32=function(){return 4294967296*e.next()|0},i.double=function(){return i()+1.1102230246251565e-16*(2097152*i()|0)},i.quick=i,o&&("object"==typeof o&&r(o,e),i.state=function(){return r(e,{})}),i}n&&n.exports?n.exports=o:this.alea=o}(0,t)}),rt=nt(function(t){!function(t,n,e){function r(t,n){return n.x=t.x,n.y=t.y,n.z=t.z,n.w=t.w,n}function o(t,n){var e=new function(t){var n=this,e="";n.x=0,n.y=0,n.z=0,n.w=0,n.next=function(){var t=n.x^n.x<<11;return n.x=n.y,n.y=n.z,n.z=n.w,n.w^=n.w>>>19^t^t>>>8},t===(0|t)?n.x=t:e+=t;for(var r=0;r>>0)/4294967296};return i.double=function(){do{var t=((e.next()>>>11)+(e.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},i.int32=e.next,i.quick=i,o&&("object"==typeof o&&r(o,e),i.state=function(){return r(e,{})}),i}n&&n.exports?n.exports=o:this.xor128=o}(0,t)}),ot=nt(function(t){!function(t,n,e){function r(t,n){return n.x=t.x,n.y=t.y,n.z=t.z,n.w=t.w,n.v=t.v,n.d=t.d,n}function o(t,n){var e=new function(t){var n=this,e="";n.next=function(){var t=n.x^n.x>>>2;return n.x=n.y,n.y=n.z,n.z=n.w,n.w=n.v,(n.d=n.d+362437|0)+(n.v=n.v^n.v<<4^t^t<<1)|0},n.x=0,n.y=0,n.z=0,n.w=0,n.v=0,t===(0|t)?n.x=t:e+=t;for(var r=0;r>>4),n.next()}(t),o=n&&n.state,i=function(){return(e.next()>>>0)/4294967296};return i.double=function(){do{var t=((e.next()>>>11)+(e.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},i.int32=e.next,i.quick=i,o&&("object"==typeof o&&r(o,e),i.state=function(){return r(e,{})}),i}n&&n.exports?n.exports=o:this.xorwow=o}(0,t)}),it=nt(function(t){!function(t,n,e){function r(t,n){return n.x=t.x.slice(),n.i=t.i,n}function o(t,n){null==t&&(t=+new Date);var e=new function(t){var n=this;n.next=function(){var t,e,r=n.x,o=n.i;return t=r[o],e=(t^=t>>>7)^t<<24,e^=(t=r[o+1&7])^t>>>10,e^=(t=r[o+3&7])^t>>>3,e^=(t=r[o+4&7])^t<<7,t=r[o+7&7],e^=(t^=t<<13)^t<<9,r[o]=e,n.i=o+1&7,e},function(t,n){var e,r=[];if(n===(0|n))r[0]=n;else for(n=""+n,e=0;e0;--e)t.next()}(n,t)}(t),o=n&&n.state,i=function(){return(e.next()>>>0)/4294967296};return i.double=function(){do{var t=((e.next()>>>11)+(e.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},i.int32=e.next,i.quick=i,o&&(o.x&&r(o,e),i.state=function(){return r(e,{})}),i}n&&n.exports?n.exports=o:this.xorshift7=o}(0,t)}),at=nt(function(t){!function(t,n,e){function r(t,n){return n.i=t.i,n.w=t.w,n.X=t.X.slice(),n}function o(t,n){null==t&&(t=+new Date);var e=new function(t){var n=this;n.next=function(){var t,e,r=n.w,o=n.X,i=n.i;return n.w=r=r+1640531527|0,e=o[i+34&127],t=o[i=i+1&127],e^=e<<13,t^=t<<17,e^=e>>>15,t^=t>>>12,e=o[i]=e^t,n.i=i,e+(r^r>>>16)|0},function(t,n){var e,r,o,i,a,u=[],s=128;for(n===(0|n)?(r=n,n=null):(n+="\0",r=0,s=Math.max(s,n.length)),o=0,i=-32;i>>15,r^=r<<4,r^=r>>>13,i>=0&&(a=a+1640531527|0,o=0==(e=u[127&i]^=r+a)?o+1:0);for(o>=128&&(u[127&(n&&n.length||0)]=-1),o=127,i=512;i>0;--i)r=u[o+34&127],e=u[o=o+1&127],r^=r<<13,e^=e<<17,r^=r>>>15,e^=e>>>12,u[o]=r^e;t.w=a,t.X=u,t.i=o}(n,t)}(t),o=n&&n.state,i=function(){return(e.next()>>>0)/4294967296};return i.double=function(){do{var t=((e.next()>>>11)+(e.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},i.int32=e.next,i.quick=i,o&&(o.X&&r(o,e),i.state=function(){return r(e,{})}),i}n&&n.exports?n.exports=o:this.xor4096=o}(0,t)}),ut=nt(function(t){!function(t,n,e){function r(t,n){return n.a=t.a,n.b=t.b,n.c=t.c,n.d=t.d,n}function o(t,n){var e=new function(t){var n=this,e="";n.next=function(){var t=n.b,e=n.c,r=n.d,o=n.a;return t=t<<25^t>>>7^e,e=e-r|0,r=r<<24^r>>>8^o,o=o-t|0,n.b=t=t<<20^t>>>12^e,n.c=e=e-r|0,n.d=r<<16^e>>>16^o,n.a=o-t|0},n.a=0,n.b=0,n.c=-1640531527,n.d=1367130551,t===Math.floor(t)?(n.a=t/4294967296|0,n.b=0|t):e+=t;for(var r=0;r>>0)/4294967296};return i.double=function(){do{var t=((e.next()>>>11)+(e.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},i.int32=e.next,i.quick=i,o&&("object"==typeof o&&r(o,e),i.state=function(){return r(e,{})}),i}n&&n.exports?n.exports=o:this.tychei=o}(0,t)}),st=nt(function(t){!function(n,e){var r,o=this,i=256,a=6,u="random",s=e.pow(i,a),l=e.pow(2,52),c=2*l,f=i-1;function h(t,h,m){var g=[],y=d(function t(n,e){var r,o=[],i=typeof n;if(e&&"object"==i)for(r in n)try{o.push(t(n[r],e-1))}catch(t){}return o.length?o:"string"==i?n:n+"\0"}((h=1==h?{entropy:!0}:h||{}).entropy?[t,v(n)]:null==t?function(){try{var t;return r&&(t=r.randomBytes)?t=t(i):(t=new Uint8Array(i),(o.crypto||o.msCrypto).getRandomValues(t)),v(t)}catch(t){var e=o.navigator,a=e&&e.plugins;return[+new Date,o,a,o.screen,v(n)]}}():t,3),g),x=new function(t){var n,e=t.length,r=this,o=0,a=r.i=r.j=0,u=r.S=[];for(e||(t=[e++]);o=c;)t/=2,n/=2,e>>>=1;return(t+e)/n};return w.int32=function(){return 0|x.g(4)},w.quick=function(){return x.g(4)/4294967296},w.double=w,d(v(x.S),n),(h.pass||m||function(t,n,r,o){return o&&(o.S&&p(o,x),t.state=function(){return p(x,{})}),r?(e[u]=t,n):t})(w,y,"global"in h?h.global:this==e,h.state)}function p(t,n){return n.i=t.i,n.j=t.j,n.S=t.S.slice(),n}function d(t,n){for(var e,r=t+"",o=0;o=1||0===a);var u=Math.sqrt(-2*Math.log(a)/a);n=this.mean+this.stdDev*o*u,e=this.mean+this.stdDev*i*u,this.truncated&&!this.isValidTruncated(n)||(r=!0)}return this.truncated&&!this.isValidTruncated(e)||(this.nextVal=this.convertValue(e)),this.convertValue(n)},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}();(dt=lt||(lt={})).float32="float32",dt.int32="int32",dt.bool="bool",function(t){t.R0="R0",t.R1="R1",t.R2="R2",t.R3="R3",t.R4="R4",t.R5="R5",t.R6="R6"}(ct||(ct={})),function(t){t.float32="float32",t.int32="int32",t.bool="int32"}(ft||(ft={})),function(t){t.float32="float32",t.int32="int32",t.bool="bool"}(ht||(ht={})),function(t){t.float32="float32",t.int32="float32",t.bool="float32"}(pt||(pt={}));var gt={float32:pt,int32:ft,bool:ht};function yt(t,n){return gt[t][n]}function xt(t){return yt(t,"int32")}function wt(t,n){for(var e=t.length,r=[],o=0;o1&&1===a&&r.unshift(i)}return r}function bt(t,n){for(var e=[],r=0;r1)&&e.unshift(i)}return e}function Et(t,n){for(var e=[],r="Operands could not be broadcast together with shapes "+t+" and "+n+".",o=Math.max(t.length,n.length),i=0;i1&&u>1&&a!==u)throw Error(r);e.unshift(Math.max(a,u))}return e}var St=function(){function t(){}return t.add=function(t,n){s({a:t,b:n},"add"),d(t,n);var e=Et(t.shape,n.shape);return Xo.engine.runKernel(function(e){return e.add(t,n)},{a:t,b:n},function(r){return{a:function(){var n=r,o=bt(t.shape,e);return o.length>0&&(n=n.sum(o)),n.reshape(t.shape)},b:function(){var t=r,o=bt(n.shape,e);return o.length>0&&(t=t.sum(o)),t.reshape(n.shape)}}})},t.addStrict=function(t,n){return p(t.shape,n.shape,"Error in addStrict: "),t.add(n)},t.sub=function(t,n){s({a:t,b:n},"sub"),d(t,n);var e=Et(t.shape,n.shape);return Xo.engine.runKernel(function(e){return e.subtract(t,n)},{a:t,b:n},function(r){return{a:function(){var n=r,o=bt(t.shape,e);return o.length>0&&(n=n.sum(o)),n.reshape(t.shape)},b:function(){var t=r,o=bt(n.shape,e);return o.length>0&&(t=t.sum(o)),t.neg().reshape(n.shape)}}})},t.subStrict=function(t,n){return p(t.shape,n.shape,"Error in subStrict: "),t.sub(n)},t.pow=function(t,n){s({base:t,exp:n},"pow");var e=Et(t.shape,n.shape);return t=t.cast(yt(t.dtype,n.dtype)),n=n.cast(yt(t.dtype,n.dtype)),Xo.engine.runKernel(function(e,r){return r(e.pow(t,n))},{base:t,exp:n},function(r,o){var i=o[0];return{base:function(){var o=r.mul(n.toFloat().mul(i.div(t))),a=bt(t.shape,e);return a.length>0&&(o=o.sum(a)),o.reshape(t.shape)},exp:function(){var o=r.mul(i.mul(t.log()).toFloat()),a=bt(n.shape,e);return a.length>0&&(o=o.sum(a)),o.reshape(n.shape)}}})},t.powStrict=function(t,n){return p(t.shape,n.shape,"Error in powStrict: "),t.pow(n)},t.mul=function(t,n){s({a:t,b:n},"mul"),d(t,n);var e=Et(t.shape,n.shape);return Xo.engine.runKernel(function(e){return e.multiply(t,n)},{a:t,b:n},function(r){return{a:function(){var o=r.mul(n.toFloat()),i=bt(t.shape,e);return i.length>0?o.sum(i).reshape(t.shape):o},b:function(){var o=r.mul(t.toFloat()),i=bt(n.shape,e);return i.length>0?o.sum(i).reshape(n.shape):o}}})},t.mulStrict=function(t,n){return p(t.shape,n.shape,"Error in multiplyStrict: "),t.mul(n)},t.div=function(n,e){var r;if(s({a:n,b:e},"div"),d(n,e),"int32"===n.dtype&&"int32"===e.dtype)return t.floorDiv(n,e);r=function(t){return t.realDivide(n,e)};var o=Et(n.shape,e.shape);return Xo.engine.runKernel(r,{a:n,b:e},function(t){return{a:function(){var r=t.div(e.toFloat()),i=bt(n.shape,o);return i.length>0?r.sum(i).reshape(n.shape):r},b:function(){var r=t.mul(n.toFloat()),i=bt(e.shape,o);i.length>0&&(r=r.sum(i).reshape(e.shape));var a=e.square();return r.div(a.toFloat()).neg()}}})},t.floorDiv=function(t,n){s({a:t,b:n},"floorDiv"),d(t,n);var e=Et(t.shape,n.shape);return Xo.engine.runKernel(function(e){return e.floorDiv(t,n)},{a:t,b:n},function(r){return{a:function(){var o=r.div(n.toFloat()),i=bt(t.shape,e);return i.length>0?o.sum(i).reshape(t.shape):o},b:function(){var o=r.mul(t.toFloat()),i=bt(n.shape,e);i.length>0&&(o=o.sum(i).reshape(n.shape));var a=n.square();return o.div(a.toFloat()).neg()}}})},t.divStrict=function(t,n){return p(t.shape,n.shape,"Error in divideStrict: "),t.div(n)},t.mod=function(t,n){s({a:t,b:n},"mod"),d(t,n);var e=Et(t.shape,n.shape);return Xo.engine.runKernel(function(e){return e.mod(t,n)},{a:t,b:n},function(r){return{a:function(){var n=bt(t.shape,e);return n.length>0?r.sum(n).reshape(t.shape):r},b:function(){var o=r.mul(t.div(n).floor().neg()),i=bt(n.shape,e);return i.length>0?o.sum(i).reshape(n.shape):o}}})},t.modStrict=function(t,n){return p(t.shape,n.shape,"Error in modStrict: "),t.mod(n)},t.minimum=function(t,n){return s({a:t,b:n},"minimum"),d(t,n),"bool"===t.dtype&&(t=t.toInt()),"bool"===n.dtype&&(n=n.toInt()),Et(t.shape,n.shape),Xo.engine.runKernel(function(e){return e.minimum(t,n)},{a:t,b:n},function(e){return{a:function(){return e.mul(t.lessEqual(n).toFloat())},b:function(){return e.mul(t.greater(n).toFloat())}}})},t.minimumStrict=function(t,n){return p(t.shape,n.shape,"Error in minimumStrict: "),t.minimum(n)},t.maximum=function(t,n){return s({a:t,b:n},"maximum"),d(t,n),"bool"===t.dtype&&(t=t.toInt()),"bool"===n.dtype&&(n=n.toInt()),Et(t.shape,n.shape),Xo.engine.runKernel(function(e){return e.maximum(t,n)},{a:t,b:n},function(e){return{a:function(){return e.mul(t.greaterEqual(n).toFloat())},b:function(){return e.mul(t.less(n).toFloat())}}})},t.maximumStrict=function(t,n){return p(t.shape,n.shape,"Error in minimumStrict: "),t.maximum(n)},t.squaredDifference=function(t,n){return s({a:t,b:n},"squaredDifference"),d(t,n),Et(t.shape,n.shape),Xo.engine.runKernel(function(e){return e.squaredDifference(t,n)},{a:t,b:n},function(e){var r=qr(2);return{a:function(){return e.mul(t.sub(n).mul(r))},b:function(){return e.mul(n.sub(t).mul(r))}}})},t.squaredDifferenceStrict=function(t,n){return p(t.shape,n.shape,"Error in squaredDifferenceStrict: "),t.squaredDifference(n)},t.atan2=function(n,e){s({a:n,b:e},"atan2"),d(n,e);var r=Et(n.shape,e.shape);return Xo.engine.runKernel(function(t){return t.atan2(n,e)},{a:n,b:e},function(o){return{a:function(){var i=t.add(Xe(n),Xe(e)),a=o.mul(e.div(i)),u=bt(n.shape,r);return u.length>0&&(a=a.sum(u)),a.reshape(n.shape)},b:function(){var i=t.add(Xe(n),Xe(e)),a=Fe(o.mul(n.div(i))),u=bt(e.shape,r);return u.length>0&&(a=a.sum(u)),a.reshape(e.shape)}}})},r([function(){for(var t=[],n=0;n2)throw new Error("Rank of probabilities must be 1 or 2, but is "+i);e=e||Math.random();var a=1===i?t.as2D(1,-1):t,u=Xo.engine.runKernel(function(t){return t.multinomial(a,r,n,e)},{logits2D:a});return 1===i?u.as1D():u},t.oneHot=function(t,n,e,r){if(void 0===e&&(e=1),void 0===r&&(r=0),h("int32"===t.dtype,"Indices must be of dtype `int32`"),n<2)throw new Error("Error in oneHot: depth must be >=2, but it is "+n);return Xo.engine.runKernel(function(o){return o.oneHot(t,n,e,r)},{indices:t})},t.fromPixels=function(t,n){if(void 0===n&&(n=3),n>4)throw new Error("Cannot construct Tensor with more than 4 channels from pixels.");return Xo.engine.fromPixels(t,n)},t.toPixels=function(t,n){return o(this,void 0,void 0,function(){var e,r,o,a,u,l,c,f,h,p,d,v,m,g,y,x,w,b,E;return i(this,function(i){switch(i.label){case 0:if(s({img:t},"toPixels"),2!==t.rank&&3!==t.rank)throw new Error("toPixels only supports rank 2 or 3 tensors, got rank "+t.rank+".");if(e=t.shape.slice(0,2),r=e[0],o=e[1],(a=2===t.rank?1:t.shape[2])>4||2===a)throw new Error("toPixels only supports depth of size 1, 3 or 4 but got "+a);return u=t.min(),l=t.max(),[4,u.data()];case 1:return c=i.sent()[0],[4,l.data()];case 2:if(f=i.sent()[0],u.dispose(),l.dispose(),"float32"===t.dtype){if(c<0||f>1)throw new Error("Tensor values for a float32 Tensor must be in the range [0 - 1] but got range ["+c+" - "+f+"].")}else{if("int32"!==t.dtype)throw new Error("Unsupported type for toPixels: "+t.dtype+". Please use float32 or int32 tensors.");if(c<0||f>255)throw new Error("Tensor values for a int32 Tensor must be in the range [0 - 255] but got range ["+c+" - "+f+"].")}return[4,t.data()];case 3:for(h=i.sent(),p="float32"===t.dtype?255:1,d=new Uint8ClampedArray(o*r*4),v=0;v=1,"Pass at least one tensor to tf.stack"),1===t.length)return t[0].expandDims(n);var e=t[0].rank,r=t[0].shape,o=t[0].dtype;h(n<=e,"Axis must be <= rank of the tensor"),t.forEach(function(t){p(r,t.shape,"All tensors passed to stack must have matching shapes")}),t.forEach(function(t){h(o===t.dtype,"All tensors passed to stack must have matching dtypes")});var i=t.map(function(t){return t.expandDims(n)});return $.concat(i,n)},t.unstack=function(t,n){void 0===n&&(n=0);for(var e,r=t.shape[n],o=Array(t.rank-1).fill(0),i=0,a=0;a1)return t.zeros([0],o);var i=Tt(Math.abs(Math.ceil((e-n)/r)),o);e0,"Gram-Schmidt process: input must not be null, undefined, or empty");for(var e=t[0].shape[0],r=1;r0)for(var e=0;e0?o>=u[n]:o<=u[n]);o+=r[n])e+=1;return e}),[a,l]}function en(t,n,e,r,o){var i=n[o];t&1<0?Number.MIN_SAFE_INTEGER:Number.MAX_SAFE_INTEGER);var a=r[o];return i<0&&(i+=a),c(0,i,a-1)}function rn(t,n,e,r,o){var i=n[o];t&1<0?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER);var a=r[o];return i<0&&(i+=a),e[o]>0?c(0,i,a):c(-1,i,a-1)}var on=function(){function t(){}return t.slice1d=function(n,e,r){return h(1===n.rank,"slice1d expects a rank-1 tensor, but got a rank-"+n.rank+" tensor"),t.slice(n,[e],[r])},t.slice2d=function(n,e,r){return h(2===n.rank,"slice1d expects a rank-2 tensor, but got a rank-"+n.rank+" tensor"),t.slice(n,e,r)},t.slice3d=function(n,e,r){return h(3===n.rank,"slice1d expects a rank-3 tensor, but got a rank-"+n.rank+" tensor"),t.slice(n,e,r)},t.slice4d=function(n,e,r){return h(4===n.rank,"slice1d expects a rank-4 tensor, but got a rank-"+n.rank+" tensor"),t.slice(n,e,r)},t.slice=function(t,n,e){if(s({x:t},"slice"),0===t.rank)throw new Error("Slicing scalar is not possible");var r,o;r="number"==typeof n?[n].concat(new Array(t.rank-1).fill(0)):n.length=0?n:(h(-1===n,"Bad value in size"),t.shape[e]-r[e])}),function(t,n,e){h(t.rank===n.length,"Error in slice"+t.rank+"D: Length of begin "+n+" must match the rank of the array ("+t.rank+")."),h(t.rank===e.length,"Error in slice"+t.rank+"D: Length of size "+e+" must match the rank of the array ("+t.rank+").");for(var r=0;r=0&&e=0;--r)e[r]=e[r+1]*t[r+1];return e}var So=function(){function t(){}return t.gradScope=function(t,n){return Co(t,n,!0)},t.grad=function(t){return h(O(t),"The f passed in grad(f) must be a function"),function(n,e){return h(n instanceof xo,"The x passed in grad(f)(x) must be a tensor"),h(null==e||e instanceof xo,"The dy passed in grad(f)(x, dy) must be a tensor"),Co(function(){var r=Xo.engine.gradients(function(){return t(n)},[n],e),o=r.value,i=r.grads;return null!=e&&p(o.shape,e.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),Ro(i),i[0]})}},t.grads=function(t){return h(O(t),"The f passed in grads(f) must be a function"),function(n,e){return h(Array.isArray(n)&&n.every(function(t){return t instanceof xo}),"The args passed in grads(f)(args) must be an array of tensors"),h(null==e||e instanceof xo,"The dy passed in grads(f)(args, dy) must be a tensor"),Co(function(){var r=Xo.engine.gradients(function(){return t.apply(void 0,n)},n,e),o=r.value,i=r.grads;return null!=e&&p(o.shape,e.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Ro(i),i})}},t.valueAndGrad=function(t){return h(O(t),"The f passed in valueAndGrad(f) must be a function"),function(n,e){h(n instanceof xo,"The x passed in valueAndGrad(f)(x) must be a tensor"),h(null==e||e instanceof xo,"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");var r=Xo.engine.gradients(function(){return t(n)},[n],e),o=r.grads,i=r.value;return Ro(o),{grad:o[0],value:i}}},t.valueAndGrads=function(t){return h(O(t),"The f passed in valueAndGrads(f) must be a function"),function(n,e){h(Array.isArray(n)&&n.every(function(t){return t instanceof xo}),"The args passed in valueAndGrads(f)(args) must be array of tensors"),h(null==e||e instanceof xo,"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");var r=Xo.engine.gradients(function(){return t.apply(void 0,n)},n,e);return null!=e&&p(r.value.shape,e.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Ro(r.grads),r}},t.variableGrads=function(t,n){if(h(O(t),"The f passed in variableGrads(f) must be a function"),h(null==n||Array.isArray(n)&&n.every(function(t){return t instanceof wo}),"The varList passed in variableGrads(f, varList) must be an array of variables"),null==n)for(var e in n=[],Xo.engine.registeredVariables)n.push(Xo.engine.registeredVariables[e]);var r=n.length;h((n=n.filter(function(t){return t.trainable})).length>0,"variableGrads() expects at least one of the input variables to be trainable, but none of the "+r+" variables is trainable.");var o=Xo.engine.gradients(t,n,null,!0),i=o.value,a=o.grads;h(a.some(function(t){return null!=t}),"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()."),h(0===i.rank,"The f passed in variableGrads(f) must return a scalar, but it returned a rank-"+i.rank+" tensor");var u={};return n.forEach(function(t,n){null!=a[n]&&(u[t.name]=a[n])}),{value:i,grads:u}},t.customGrad=function(t){return Xo.engine.customGrad(t)},r([function(){for(var t=[],n=0;n0)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 Co=Gt.tidy,_o=Gt.keep,ko=Gt.dispose,To=Gt.time,Io=So.grad,Do=So.valueAndGrad,Ao=So.grads,Mo=So.valueAndGrads,No=So.variableGrads,Oo=So.customGrad,Lo=function(){function t(t,n){this.backendTimer=t,this.logger=n,null==n&&(this.logger=new Fo)}return t.prototype.profileKernel=function(t,n){var e,r=this,o=this.backendTimer.time(function(){e=n()}),i=e.dataSync();return I(i,e.dtype,t),o.then(function(n){r.logger.logKernelProfile(t,e,i,n.kernelMs)}),e},t}(),Fo=function(){function t(){}return t.prototype.logKernelProfile=function(t,n,e,r){var o=E(r+"ms",9),i=E(t,25),a=n.rank,u=n.size,s=E(n.shape.toString(),14);console.log("%c"+i+"\t%c"+o+"\t%c"+a+"D "+s+"\t%c"+u,"font-weight:bold","color:red","color:blue","color: orange")},t}();var Bo,Po=function(){function t(t,n){this.backend=t,this.safeMode=n,this.registeredVariables={},this.refCounter=new WeakMap,this.nextTapeNodeId=0,this.numBytes=0,this.numTensors=0,this.numDataBuffers=0,this.gradientScopeCount=0,this.customGradientDepth=0,this.keepTensors=new Set,this.activeScope={track:[]},this.scopeStack=[this.activeScope],this.profiler=new Lo(t)}return t.prototype.runKernel=function(t,n,e){var r,o=this,i=[],a=function(t){return i.push(t),t},u=this.activeScope.name;if(this.customGradientDepth++,r=Xo.get("DEBUG")?this.profiler.profileKernel(u,function(){return t(o.backend,a)}):t(this.backend,a),this.customGradientDepth--,this.shouldRecord()){var s={id:this.nextTapeNodeId++,name:u,inputs:n,output:r};null!=e&&(s.gradient=function(t){return e(t,i)}),this.activeTape.push(s)}return r},t.prototype.registerTensor=function(t){var n=this.refCounter.has(t.dataId)?this.refCounter.get(t.dataId):0;this.numTensors++,0===n&&(this.numDataBuffers++,this.numBytes+=g(t.shape)*N(t.dtype),this.backend.register(t.dataId,t.shape,t.dtype)),this.refCounter.set(t.dataId,n+1),t instanceof wo||this.track(t)},t.prototype.registerVariable=function(t){if(null!=this.registeredVariables[t.name])throw new Error("Variable with name "+t.name+" was already registered");this.registeredVariables[t.name]=t},t.prototype.disposeTensor=function(t){if(this.refCounter.has(t.dataId)){this.keepTensors.has(t.id)&&this.keepTensors.delete(t.id),this.numTensors--;var n=this.refCounter.get(t.dataId);n<=1?(this.refCounter.delete(t.dataId),this.backend.disposeData(t.dataId),this.numDataBuffers--,this.numBytes-=g(t.shape)*N(t.dtype)):this.refCounter.set(t.dataId,n-1)}},t.prototype.disposeVariables=function(){for(var t in this.registeredVariables){var n=this.registeredVariables[t];this.disposeTensor(n),delete this.registeredVariables[t]}},t.prototype.memory=function(){var t=this.backend.memory();return t.numTensors=this.numTensors,t.numDataBuffers=this.numDataBuffers,t.numBytes=this.numBytes,t},t.prototype.shouldRecord=function(){return null!=this.activeTape&&0===this.customGradientDepth},t.prototype.addTapeNode=function(t,n,e){var r={};t.forEach(function(t,n){r[n]=t});var o={id:this.nextTapeNodeId++,name:this.activeScope.name,inputs:r,output:n,gradient:function(t){var n={};return e(t).forEach(function(t,e){n[e]=function(){return t}}),n}};this.activeTape.push(o)},t.prototype.keep=function(t){if(1===this.scopeStack.length&&Xo.engine.safeMode)throw new Error("Safe mode is ON. Enclose all tensor operations inside tf.tidy(): tf.tidy(() => {...}) to avoid memory leaks.");return this.keepTensors.add(t.id),t},t.prototype.startScope=function(t,n){void 0===n&&(n=!1),n&&0===this.gradientScopeCount&&(this.activeTape=[]),n&&this.gradientScopeCount++;var e={track:[]};t&&(e.name=t),this.scopeStack.push(e),this.activeScope=e},t.prototype.endScope=function(t,n){var e=this;void 0===n&&(n=!1),n&&(this.gradientScopeCount--,0===this.gradientScopeCount&&(this.activeTape=null));var r=new Set(this.keepTensors),o=L(t);o.forEach(function(t){return r.add(t.id)});for(var i=0;i0,"gradients() received an empty list of xs."),Co("gradients",function(){var i=t();h(i instanceof xo,"The result y returned by f() must be a tensor.");var a=function(t,n,e){for(var r={},o={},i=0;i=0;i--){a=(v=t[i]).inputs;var p=[];for(p.push(v.output),c=0;c0)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.");var u={};return u[i.id]=null==e?Rr(i.shape):e,function(t,n){for(var e=n.length-1;e>=0;e--){var r=n[e],o=t[r.output.id];if(null==r.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+r.name+".");var i=r.gradient(o);for(var a in r.inputs){if(!(a in i))throw new Error("Cannot backprop through input "+a+". Available gradients found: "+Object.keys(i)+".");var u=i[a](),s=r.inputs[a];if(!y(u.shape,s.shape))throw new Error("Error in gradient for op "+r.name+". The gradient of input '"+a+"' has shape '"+u.shape+"', which does not match the shape of the input '"+s.shape+"'");if(null==t[s.id])t[s.id]=u;else{var l=t[s.id];t[s.id]=l.add(u),l.dispose()}}}}(u,a),{value:i,grads:n.map(function(t){return u[t.id]})}},!0)},t.prototype.customGrad=function(t){var n=this;return h(O(t),"The f passed in customGrad(f) must be a function."),function(){for(var e,r=[],o=0;o {op();...}); to avoid memory leaks.");return this.activeScope.track.push(t),t},t}();!function(t){t[t.NUMBER=0]="NUMBER",t[t.BOOLEAN=1]="BOOLEAN",t[t.STRING=2]="STRING"}(Bo||(Bo={}));var Uo=[{name:"DEBUG",type:Bo.BOOLEAN},{name:"IS_BROWSER",type:Bo.BOOLEAN},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",type:Bo.NUMBER},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",type:Bo.BOOLEAN},{name:"WEBGL_VERSION",type:Bo.NUMBER},{name:"WEBGL_RENDER_FLOAT32_ENABLED",type:Bo.BOOLEAN},{name:"WEBGL_DOWNLOAD_FLOAT_ENABLED",type:Bo.BOOLEAN},{name:"WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED",type:Bo.BOOLEAN},{name:"BACKEND",type:Bo.STRING}];function Wo(t,n){return null!=t.getExtension(n)}function zo(t){if(0===t||!Xo.get("IS_BROWSER"))throw new Error("Cannot get WebGL rendering context, WebGL is disabled.");var n=document.createElement("canvas");return 1===t?n.getContext("webgl")||n.getContext("experimental-webgl"):n.getContext("webgl2")}function Vo(t){if(null!=t){var n=t.getExtension("WEBGL_lose_context");if(null==n)throw new Error("Extension WEBGL_lose_context not supported on this browser.");n.loseContext()}}function Go(t){var n;try{n=zo(t)}catch(t){return!1}return null!=n&&(Vo(n),!0)}function qo(t,n){var e=t.createFramebuffer(),r=t.createTexture();t.bindTexture(t.TEXTURE_2D,r);var o=2===n?t.RGBA32F:t.RGBA;t.texImage2D(t.TEXTURE_2D,0,o,1,1,0,t.RGBA,t.FLOAT,null),t.bindFramebuffer(t.FRAMEBUFFER,e),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,r,0)}var Ho=function(){function t(t){this.features={},this.registry={},null!=t&&(this.features=t),this.get("DEBUG")&&console.warn("Debugging mode is ON. The output of every math call will be downloaded to CPU and checked for NaNs. This significantly impacts performance.")}return t.setBackend=function(t,n){if(void 0===n&&(n=!1),!(t in Xo.registry))throw new Error("Backend type '"+t+"' not found in registry");Xo.initBackend(t,n)},t.getBackend=function(){return Xo.initDefaultBackend(),Xo.currentBackend},t.disposeVariables=function(){Xo.engine.disposeVariables()},t.memory=function(){return Xo.engine.memory()},t.prototype.get=function(t){return t in this.features?this.features[t]:(this.features[t]=this.evaluateFeature(t),this.features[t])},t.prototype.getFeatures=function(){return this.features},t.prototype.set=function(t,n){this.features[t]=n},t.prototype.getBestBackendType=function(){var t=this;if(0===Object.keys(this.registry).length)throw new Error("No backend found in registry.");return Object.keys(this.registry).map(function(n){return{name:n,entry:t.registry[n]}}).sort(function(t,n){return n.entry.priority-t.entry.priority})[0].name},t.prototype.evaluateFeature=function(t){if("DEBUG"===t)return!1;if("IS_BROWSER"===t)return"undefined"!=typeof window;if("IS_NODE"===t)return"undefined"!=typeof process&&void 0!==process.versions.node;if("IS_CHROME"===t)return null!=navigator&&null!=navigator.userAgent&&/Chrome/.test(navigator.userAgent)&&/Google Inc/.test(navigator.vendor);if("BACKEND"===t)return this.getBestBackendType();if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"===t){var n=this.get("WEBGL_VERSION");return 0===n?0:function(t){if(0===t)return 0;var n,e=zo(t);return n=Wo(e,"EXT_disjoint_timer_query_webgl2")&&2===t?2:Wo(e,"EXT_disjoint_timer_query")?1:0,null!=e&&Vo(e),n}(n)}if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE"===t)return this.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0&&!function(){var t=navigator.userAgent||navigator.vendor||window.opera;return/(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))}();if("WEBGL_VERSION"===t)return Go(2)?2:Go(1)?1:0;if("WEBGL_RENDER_FLOAT32_ENABLED"===t)return function(t){if(0===t)return!1;var n=zo(t);if(1===t){if(!Wo(n,"OES_texture_float"))return!1}else if(!Wo(n,"EXT_color_buffer_float"))return!1;qo(n,t);var e=n.checkFramebufferStatus(n.FRAMEBUFFER)===n.FRAMEBUFFER_COMPLETE;return Vo(n),e}(this.get("WEBGL_VERSION"));if("WEBGL_DOWNLOAD_FLOAT_ENABLED"===t)return function(t){if(0===t)return!1;var n=zo(t);if(1===t){if(!Wo(n,"OES_texture_float"))return!1}else if(!Wo(n,"EXT_color_buffer_float"))return!1;qo(n,t),n.readPixels(0,0,1,1,n.RGBA,n.FLOAT,new Float32Array(4));var e=n.getError()===n.NO_ERROR;return Vo(n),e}(this.get("WEBGL_VERSION"));if("WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED"===t)return function(t){if(t>0)return!1;if(2!==t)return!1;var n=zo(t),e=Wo(n,"WEBGL_get_buffer_sub_data_async");return Vo(n),e}(this.get("WEBGL_VERSION"));if("TEST_EPSILON"===t)return this.get("WEBGL_RENDER_FLOAT32_ENABLED")?.001:.1;throw new Error("Unknown feature "+t+".")},t.prototype.setFeatures=function(t){this.features=t},t.prototype.reset=function(){this.features=Ko(),null!=this.globalEngine&&(this.globalEngine=null)},t.prototype.initBackend=function(t,n){void 0===n&&(n=!1),this.currentBackend=t;var e=Xo.findBackend(t);this.globalEngine=new Po(e,n)},t.prototype.findBackend=function(t){return t in this.registry?this.registry[t].backend:null},t.prototype.registerBackend=function(t,n,e){void 0===e&&(e=1),t in this.registry&&console.warn(t+" backend was already registered");try{var r=n();return this.registry[t]={backend:r,priority:e},!0}catch(n){return console.warn("Registration of backend "+t+" failed"),console.warn(n.stack||n.message),!1}},t.prototype.removeBackend=function(t){if(!(t in this.registry))throw new Error(t+" backend not found in registry");this.registry[t].backend.dispose(),delete this.registry[t]},Object.defineProperty(t.prototype,"engine",{get:function(){return this.initDefaultBackend(),this.globalEngine},enumerable:!0,configurable:!0}),t.prototype.initDefaultBackend=function(){null==this.globalEngine&&this.initBackend(Xo.get("BACKEND"),!1)},r([function(){for(var t=[],n=0;n":"<",s=e?"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 = 0;\n float bestValue = getA(batch, inOffset);\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 "},ni=function(t){this.variableNames=["dy"],this.outputShape=t.inShape;var n=t.filterHeight,e=t.filterWidth,r=t.strideHeight,o=t.strideWidth,i=n-1-t.padInfo.top,a=e-1-t.padInfo.left,u=1/(n*e);this.userCode="\n const ivec2 pads = ivec2("+i+", "+a+");\n const float avgMultiplier = float("+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 < "+n+"; 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 for (int wC = 0; wC < "+e+"; 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 float dyValue = getDy(b, idyR, idyC, d);\n\n dotProd += dyValue * avgMultiplier;\n }\n }\n setOutput(dotProd);\n }\n "},ei=function(t,n,e,r,o,i){this.outputShape=[],this.supportsBroadcasting=!0,this.variableNames=["x","mean","variance"],Et(t,n),Et(t,e);var a="0.0";null!=r&&(Et(t,r),this.variableNames.push("offset"),a="getOffsetAtOutCoords()");var u="1.0";null!=o&&(Et(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 = "+a+";\n float scale = "+u+";\n float inv = scale * inversesqrt(variance + float("+i+"));\n setOutput((x - mean) * inv + offset);\n }\n "},ri=function(t,n,e){this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.outputShape=Et(n,e),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 "},oi=function(t,n,e){this.variableNames=["A"],this.outputShape=t;var r=n.toFixed(20),o=e.toFixed(20);this.userCode="\n void main() {\n float value = getAAtOutCoords();\n if (isNaN(value)) {\n setOutput(value);\n return;\n }\n\n setOutput(clamp(value, "+r+", "+o+"));\n }\n "},ii=function(t,n){this.variableNames=["A","B"],this.outputShape=[],this.outputShape=J(t,n,1),this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int yR = coords.x;\n int yC = coords.y;\n\n float value = 0.0;\n if (yC < "+t[1]+") {\n value = getA(yR, yC);\n } else {\n yC -= "+t[1]+";\n value = getB(yR, yC);\n }\n\n setOutput(value);\n }\n "},ai=function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var n=t.strideHeight,e=t.strideWidth,r=t.padInfo.top,o=t.padInfo.left;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 * "+n+" - "+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 * "+e+" - "+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 "},ui=function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var n=t.filterHeight,e=t.filterWidth,r=t.strideHeight,o=t.strideWidth,i=n-1-t.padInfo.top,a=e-1-t.padInfo.left;this.userCode="\n const ivec2 pads = ivec2("+i+", "+a+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords[3];\n\n ivec2 dyCorner = coords.yz - 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 < "+n+"; 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 = "+n+" - 1 - wR;\n\n for (int wC = 0; wC < "+e+"; 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 = "+e+" - 1 - wC;\n\n for (int d2 = 0; d2 < "+t.outChannels+"; d2++) {\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n "},si=function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var n=t.strideHeight,e=t.strideWidth,r=t.padInfo.top,o=t.padInfo.left,i=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 * "+i+" + dm;\n\n float dotProd = 0.0;\n\n // TODO: 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 * "+n+" - "+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 * "+e+" - "+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 "},li=function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var n=t.filterHeight,e=t.filterWidth,r=t.strideHeight,o=t.strideWidth,i=n-1-t.padInfo.top,a=e-1-t.padInfo.left,u=t.outChannels/t.inChannels;this.userCode="\n const ivec2 pads = ivec2("+i+", "+a+");\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 < "+n+"; 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 = "+n+" - 1 - wR;\n\n for (int wC = 0; wC < "+e+"; 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 = "+e+" - 1 - wC;\n\n // TODO: 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 "},ci=function(t){this.variableNames=["x","W"],this.outputShape=t.outShape;var n=t.padInfo.top,e=t.padInfo.left,r=t.strideHeight,o=t.strideWidth,i=t.dilationHeight,a=t.dilationWidth,u=t.filterHeight,s=t.filterWidth,l=4*Math.floor(t.inChannels/4),c=t.inChannels%4;this.userCode="\n const ivec2 strides = ivec2("+r+", "+o+");\n const ivec2 pads = ivec2("+n+", "+e+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d2 = coords[3];\n\n ivec2 xRCCorner = coords.yz * 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 < "+u+"; wR++) {\n int xR = xRCorner + wR * "+i+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+s+"; wC++) {\n int xC = xCCorner + wC * "+a+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n for (int d1 = 0; d1 < "+l+"; d1 += 4) {\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 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 dotProd += dot(xValues, wValues);\n }\n\n if ("+(1===c)+") {\n dotProd +=\n getX(batch, xR, xC, "+l+") *\n getW(wR, wC, "+l+", d2);\n } else if ("+(2===c)+") {\n vec2 xValues = vec2(\n getX(batch, xR, xC, "+l+"),\n getX(batch, xR, xC, "+l+" + 1)\n );\n vec2 wValues = vec2(\n getW(wR, wC, "+l+", d2),\n getW(wR, wC, "+l+" + 1, d2)\n );\n dotProd += dot(xValues, wValues);\n } else if ("+(3===c)+") {\n vec3 xValues = vec3(\n getX(batch, xR, xC, "+l+"),\n getX(batch, xR, xC, "+l+" + 1),\n getX(batch, xR, xC, "+l+" + 2)\n );\n vec3 wValues = vec3(\n getW(wR, wC, "+l+", d2),\n getW(wR, wC, "+l+" + 1, d2),\n getW(wR, wC, "+l+" + 2, d2)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n }\n setOutput(dotProd);\n }\n "},fi=function(t){this.variableNames=["x","W"],this.outputShape=t.outShape;var n=t.inHeight,e=t.inWidth,r=t.padInfo.top,o=t.padInfo.left,i=t.strideHeight,a=t.strideWidth,u=t.dilationHeight,s=t.dilationWidth,l=t.filterHeight,c=t.filterWidth,f=t.outChannels/t.inChannels;this.userCode="\n const ivec2 strides = ivec2("+i+", "+a+");\n const ivec2 pads = ivec2("+r+", "+o+");\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 / "+f+";\n int q = d2 - d1 * "+f+";\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 // TODO(dsmilkov): Flatten the two for loops and vec4 the operations.\n for (int wR = 0; wR < "+l+"; wR++) {\n int xR = xRCorner + wR * "+u+";\n\n if (xR < 0 || xR >= "+n+") {\n continue;\n }\n\n for (int wC = 0; wC < "+c+"; wC++) {\n int xC = xCCorner + wC * "+s+";\n\n if (xC < 0 || xC >= "+e+") {\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 setOutput(dotProd);\n }\n "};function hi(t,n,e,r){var o=t.map(function(t){var n=g(t.shapeInfo.logicalShape);return t.shapeInfo.isUniform?"uniform float "+t.name+(n>1?"["+n+"]":"")+";":"uniform sampler2D "+t.name+";"});o=o.join("\n");var i=t.map(function(t){return function(t,n,e){var r=function(t){var n=t.name,e="get"+n.charAt(0).toUpperCase()+n.slice(1)+"Flat",r=g(t.shapeInfo.logicalShape);if(t.shapeInfo.isUniform)return 1===r?"float "+e+"(int index) {return "+n+";}":"\n float "+e+"(int index) {\n for (int i = 0; i < "+r+"; i++) {\n if (i == index) {\n return "+n+"[i];\n }\n }\n }\n ";var o=t.shapeInfo.texShape,i=o[0],a=o[1];return 1===a&&1===i?"\n float "+e+"(int index) {\n return sampleTexture("+n+", halfCR);\n }\n ":1===a?"\n float "+e+"(int index) {\n vec2 uv = vec2(0.5, (float(index) + 0.5) / "+i+".0);\n return sampleTexture("+n+", uv);\n }\n ":1===i?"\n float "+e+"(int index) {\n vec2 uv = vec2((float(index) + 0.5) / "+a+".0, 0.5);\n return sampleTexture("+n+", uv);\n }\n ":"\n float "+e+"(int index) {\n vec2 uv = UVfrom1D("+i+", "+a+", index);\n return sampleTexture("+n+", uv);\n }\n "}(t);return r+=pi(t),(e||y(t.shapeInfo.logicalShape,n.logicalShape))&&(r+=function(t,n,e){var r=t.name,o=r.charAt(0).toUpperCase()+r.slice(1),i="get"+o+"AtOutCoords",a=wt(t.shapeInfo.logicalShape,n.logicalShape),u=t.shapeInfo.logicalShape.length,s=n.logicalShape.length,l=e&&(s>u||a.length>0),c=function(t){for(var n=0;n=1?"coords = 0;":u.map(function(t){return"coords["+(t+s)+"] = 0;"}).join("\n"))+"\n return get"+e+"("+(i<2&&o>0?"coords":t.shapeInfo.logicalShape.map(function(t,n){return"coords["+(n+s)+"]"}).join(", "))+");\n }\n "}(t,n,o,i);var h=g(t.shapeInfo.logicalShape),p="";l&&c&&(p="\n int mainPart = index / "+h+";\n index -= mainPart * "+h+";\n ");var d=n.texShape;if(f)return 1===h?"float "+i+"() {return "+r+";}":"\n float "+i+"() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+d[0]+", "+d[1]+"));\n int index = resTexRC.x * "+d[1]+" + resTexRC.y;\n "+p+"\n return get"+o+"Flat(index);\n }\n ";var v=t.shapeInfo.texShape;return y(v,d)?"\n float "+i+"() {\n return sampleTexture("+r+", resultUV);\n }\n ":"\n float "+i+"() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+d[0]+", "+d[1]+"));\n int index = resTexRC.x * "+d[1]+" + resTexRC.y;\n "+p+"\n int texR = index / "+v[1]+";\n int texC = index - texR * "+v[1]+";\n vec2 uv = (vec2(texC, texR) + halfCR) /\n vec2("+v[1]+".0, "+v[0]+".0);\n\n return sampleTexture("+r+", uv);\n }\n "}(t,n,e)),r}(t,n,r)}).join("\n"),a=n.texShape,u=function(t,n){switch(t.length){case 0:return"\n int getOutputCoords() {\n return 0;\n }\n ";case 1:return function(t,n){return 1===n[0]?"\n int getOutputCoords() {\n return int(resultUV.x * "+n[1]+".0);\n }\n ":1===n[1]?"\n int getOutputCoords() {\n return int(resultUV.y * "+n[0]+".0);\n }\n ":"\n int getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n return resTexRC.x * "+n[1]+" + resTexRC.y;\n }\n "}(0,n);case 2:return function(t,n){return y(t,n)?"\n ivec2 getOutputCoords() {\n return ivec2(resultUV.yx * vec2("+n[0]+", "+n[1]+"));\n }\n ":1===t[1]?"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n int index = resTexRC.x * "+n[1]+" + resTexRC.y;\n return ivec2(index, 0);\n }\n ":1===t[0]?"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n int index = resTexRC.x * "+n[1]+" + resTexRC.y;\n return ivec2(0, index);\n }\n ":"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n int index = resTexRC.x * "+n[1]+" + resTexRC.y;\n int r = index / "+t[1]+";\n int c = index - r * "+t[1]+";\n return ivec2(r, c);\n }\n "}(t,n);case 3:return function(t,n){var e=t[1]*t[2],r=t[2];return"\n ivec3 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n int index = resTexRC.x * "+n[1]+" + resTexRC.y;\n int r = index / "+e+";\n index -= r * "+e+";\n int c = index / "+r+";\n int d = index - c * "+r+";\n return ivec3(r, c, d);\n }\n "}(t,n);case 4:return function(t,n){var e=t[3],r=t[2]*e,o=t[1]*r;return"\n ivec4 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n int index = resTexRC.x * "+n[1]+" + resTexRC.y;\n\n int r = index / "+o+";\n index -= r * "+o+";\n\n int c = index / "+r+";\n index -= c * "+r+";\n\n int d = index / "+e+";\n int d2 = index - d * "+e+";\n\n return ivec4(r, c, d, d2);\n }\n "}(t,n);case 5:return function(t,n){var e=t[4],r=t[3]*e,o=t[2]*r,i=t[1]*o;return"\n ivec5 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx * vec2("+n[0]+",\n "+n[1]+"));\n\n int index = resTexRC.x * "+n[1]+" + resTexRC.y;\n\n int r = index / "+i+";\n index -= r * "+i+";\n\n int c = index / "+o+";\n index -= c * "+o+";\n\n int d = index / "+r+";\n index -= d * "+r+";\n\n int d2 = index / "+e+";\n int d3 = index - d2 * "+e+";\n\n ivec5 outShape = ivec5(r, c, d, d2, d3);\n return outShape;\n }\n "}(t,n);case 6:return function(t,n){var e=t[5],r=t[4]*e,o=t[3]*r,i=t[2]*o,a=t[1]*i;return"\n ivec6 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n int index = resTexRC.x * "+n[1]+" + resTexRC.y;\n\n int r = index / "+a+";\n index -= r * "+a+";\n\n int c = index / "+i+";\n index -= c * "+i+";\n\n int d = index / "+o+";\n index -= d * "+o+";\n\n int d2 = index / "+r+";\n index -= d2 * "+r+";\n\n int d3 = index / "+e+";\n int d4 = index - d3 * "+e+";\n\n ivec6 result = ivec6(r, c, d, d2, d3, d4);\n return result;\n }\n "}(t,n);default:throw new Error(t.length+"-D output sampling is not yet supported")}}(n.logicalShape,a);return[mi,di,vi,o,u,i,e].join("\n")}function pi(t){var n=t.shapeInfo.logicalShape;switch(n.length){case 0:return function(t){var n=t.name,e="get"+n.charAt(0).toUpperCase()+n.slice(1);return t.shapeInfo.isUniform?"float "+e+"() {return "+n+";}":"\n float "+e+"() {\n return sampleTexture("+n+", halfCR);\n }\n "}(t);case 1:return function(t){var n=t.name,e="get"+n.charAt(0).toUpperCase()+n.slice(1);return"\n float "+e+"(int index) {\n return "+e+"Flat(index);\n }\n "}(t);case 2:return function(t){var n=t.shapeInfo.logicalShape,e=t.name,r="get"+e.charAt(0).toUpperCase()+e.slice(1),o=t.shapeInfo.texShape;if(null!=o&&y(n,o)){var i=o[0];return"\n float "+r+"(int row, int col) {\n vec2 uv = (vec2(col, row) + halfCR) / vec2("+o[1]+".0, "+i+".0);\n return sampleTexture("+e+", uv);\n }\n "}var a=_(n),u=a.newShape,s=a.keptDims,l=u;if(l.length";this.userCode="\n int getIndex(int i) {\n "+(e?"return "+o+" -i - 1;":"return i;")+"\n }\n\n void main() {\n "+gi(r)+" coords = getOutputCoords();\n int end = "+bi(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 "+i+" end) {\n continue;\n }\n if (idx == end && "+n+") {\n continue;\n }\n "+bi(r,"coords")+" = idx;\n val += getX("+function(t,n){if(1===t)return""+n;if(2===t)return n+".x, "+n+".y";if(3===t)return n+".x, "+n+".y, "+n+".z";if(4===t)return n+".x, "+n+".y, "+n+".z, "+n+".w";throw Error("Cumulative sum for rank "+t+" is not yet supported")}(r,"coords")+");\n }\n setOutput(val);\n }\n "};function bi(t,n){if(1===t)return""+n;if(2===t)return n+".y";if(3===t)return n+".z";if(4===t)return n+".w";throw Error("Cumulative sum for rank "+t+" is not yet supported")}var Ei,Si,Ri=function(t){this.variableNames=["A"],this.outputShape=t,this.userCode="\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\n void main() {\n float x = getAAtOutCoords();\n gl_FragColor = encode_float(x);\n }\n "},Ci=function(t){this.variableNames=["A"];var n=t[0],e=t[1];this.outputShape=t,this.userCode="\n void main() {\n ivec3 coords = getOutputCoords();\n int texR = coords[0];\n int texC = coords[1];\n int depth = coords[2];\n vec2 uv = (vec2(texC, texR) + halfCR) / vec2("+e+".0, "+n+".0);\n\n vec4 values = texture2D(A, uv);\n float value;\n if (depth == 0) {\n value = values.r;\n } else if (depth == 1) {\n value = values.g;\n } else if (depth == 2) {\n value = values.b;\n } else if (depth == 3) {\n value = values.a;\n }\n\n setOutput(floor(value * 255.0 + 0.5));\n }\n "},_i=function(t,n,e){this.variableNames=["A","indices"];var r=t.slice();r[e]=n,this.outputShape=r,this.rank=r.length;var o=gi(this.rank),i=function(t,n){var e=t.length;if(e>4)throw Error("Gather for rank "+e+" is not yet supported");if(1===e)return"int(getIndices(resRC))";for(var r=["resRC.x","resRC.y","resRC.z","resRC.w"],o=[],i=0;i= "+r);for(var o=0,i=0;ir||e>r)throw o="["+n+"x"+e+"]",new Error("Requested texture size "+o+" greater than WebGL maximum on this browser / GPU ["+r+"x"+r+"].")}function $i(t){return la(t,function(){return t.createFramebuffer()},"Unable to create WebGLFramebuffer.")}function ta(t,n,e,r,o,i,a){var u=t.getAttribLocation(n,e);return-1!==u&&(Li(t,function(){return t.bindBuffer(t.ARRAY_BUFFER,r)}),Li(t,function(){return t.vertexAttribPointer(u,o,t.FLOAT,!1,i,a)}),Li(t,function(){return t.enableVertexAttribArray(u)}),!0)}function na(t,n,e){ca(t,e),Li(t,function(){return t.activeTexture(t.TEXTURE0+e)}),Li(t,function(){return t.bindTexture(t.TEXTURE_2D,n)})}function ea(t,n,e){return la(t,function(){return t.getUniformLocation(n,e)},'uniform "'+e+'" not present in program.')}function ra(t,n,e){return t.getUniformLocation(n,e)}function oa(t,n,e,r,o){Li(t,function(){return na(t,e,o)}),Li(t,function(){return t.uniform1i(r,o)})}function ia(t,n,e){Li(t,function(){return t.bindFramebuffer(t.FRAMEBUFFER,e)}),Li(t,function(){return t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,n,0)})}function aa(t,n){Li(t,function(){return t.bindFramebuffer(t.FRAMEBUFFER,n)}),Li(t,function(){return t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,null,0)})}function ua(t){var n=t.checkFramebufferStatus(t.FRAMEBUFFER);if(n!==t.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+sa(t,n))}function sa(t,n){switch(n){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 "+n}}function la(t,n,e){var r=Li(t,function(){return n()});if(null==r)throw new Error(e);return r}function ca(t,n){var e=t.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,r=n+t.TEXTURE0;if(re)throw new Error("textureUnit must be in [gl.TEXTURE0, gl.TEXTURE"+e+"].")}function fa(t,n){2!==n.length&&(n=_(n).newShape);var e=Qi(t),r=g(n);return n.length<=1&&r<=e?[r,1]:2===n.length&&n[0]<=e&&n[1]<=e?n:3===n.length&&n[0]<=e&&n[1]*n[2]<=e?[n[0],n[1]*n[2]]:4===n.length&&n[0]<=e&&n[1]*n[2]*n[3]<=e?[n[0],n[1]*n[2]*n[3]]:b(r)}var ha=Object.freeze({createWebGLRenderingContext:Ni,createWebGLRenderingContextFromCanvas:Oi,callAndCheck:Li,enableDebugWebGLErrorChecking:Bi,checkWebGLError:Pi,getWebGLErrorMessage:Ui,getExtensionOrThrow:Wi,createVertexShader:zi,createFragmentShader:Vi,createProgram:qi,linkProgram:Hi,validateProgram:ji,createStaticVertexBuffer:Ki,createStaticIndexBuffer:Xi,queryMaxTextureSize:Qi,getNumChannels:Yi,createTexture:Ji,validateTextureSize:Zi,createFramebuffer:$i,bindVertexBufferToProgramAttribute:ta,bindTextureUnit:na,unbindTextureUnit:function(t,n){ca(t,n),Li(t,function(){return t.activeTexture(t.TEXTURE0+n)}),Li(t,function(){return t.bindTexture(t.TEXTURE_2D,null)})},getProgramUniformLocationOrThrow:ea,getProgramUniformLocation:ra,bindTextureToProgramUniformSampler:oa,bindCanvasToFramebuffer:function(t){Li(t,function(){return t.bindFramebuffer(t.FRAMEBUFFER,null)}),Li(t,function(){return t.viewport(0,0,t.canvas.width,t.canvas.height)}),Li(t,function(){return t.scissor(0,0,t.canvas.width,t.canvas.height)})},bindColorTextureToFramebuffer:ia,unbindColorTextureFromFramebuffer:aa,validateFramebuffer:ua,getFramebufferErrorMessage:sa,getTextureShapeFromLogicalShape:fa});function pa(){return{alpha:!1,antialias:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1,depth:!1,stencil:!1,failIfMajorPerformanceCaveat:!0}}function da(t){var n,e={alpha:!1,antialias:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1,depth:!1,stencil:!1,failIfMajorPerformanceCaveat:!0};return Li(n=null!=t?Oi(t,e):Ni(e),function(){return n.disable(n.DEPTH_TEST)}),Li(n,function(){return n.disable(n.STENCIL_TEST)}),Li(n,function(){return n.disable(n.BLEND)}),Li(n,function(){return n.disable(n.DITHER)}),Li(n,function(){return n.disable(n.POLYGON_OFFSET_FILL)}),Li(n,function(){return n.disable(n.SAMPLE_COVERAGE)}),Li(n,function(){return n.enable(n.SCISSOR_TEST)}),Li(n,function(){return n.enable(n.CULL_FACE)}),Li(n,function(){return n.cullFace(n.BACK)}),n}function va(t){return zi(t,"\n precision highp float;\n attribute vec3 clipSpacePos;\n attribute vec2 uv;\n varying vec2 resultUV;\n\n void main() {\n gl_Position = vec4(clipSpacePos, 1);\n resultUV = uv;\n }")}function ma(t){return Ki(t,new Float32Array([-1,1,0,0,1,-1,-1,0,0,0,1,1,0,1,1,1,-1,0,1,0]))}function ga(t){return Xi(t,new Uint16Array([0,1,2,2,1,3]))}function ya(t,n){var e,r,o,i,a,u,s,l=t;return 2===Xo.get("WEBGL_VERSION")?(e=l.R32F,r=l.R16F,o=l.RGBA32F,i=l.RED,a=4,u=1,s=l.HALF_FLOAT):(e=t.RGBA,r=t.RGBA,o=l.RGBA,i=t.RGBA,a=4,u=4,s=null!=n?n.HALF_FLOAT_OES:null),{internalFormatFloat:e,internalFormatHalfFloat:r,internalFormatPackedFloat:o,textureFormatFloat:i,downloadTextureFormat:t.RGBA,downloadUnpackNumChannels:a,defaultNumChannels:u,textureTypeHalfFloat:s}}function xa(t,n,e,r,o,i){Zi(t,n,e);var a=Ji(t),u=t.TEXTURE_2D;return Li(t,function(){return t.bindTexture(u,a)}),Li(t,function(){return t.texParameteri(u,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE)}),Li(t,function(){return t.texParameteri(u,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE)}),Li(t,function(){return t.texParameteri(u,t.TEXTURE_MIN_FILTER,t.NEAREST)}),Li(t,function(){return t.texParameteri(u,t.TEXTURE_MAG_FILTER,t.NEAREST)}),Li(t,function(){return t.texImage2D(u,0,r,n,e,0,o,i,null)}),Li(t,function(){return t.bindTexture(t.TEXTURE_2D,null)}),a}function wa(t,n,e,r){var o=ki(n,e);return xa(t,o[0],o[1],r.internalFormatFloat,r.textureFormatFloat,t.FLOAT)}function ba(t,n,e,r){var o=ki(n,e);return xa(t,o[0],o[1],r.internalFormatFloat,r.textureFormatFloat,r.textureTypeHalfFloat)}function Ea(t,n,e,r){var o=ki(n,e);return xa(t,o[0],o[1],t.RGBA,t.RGBA,t.UNSIGNED_BYTE)}function Sa(t,n,e,r){var o=Di(n,e);return xa(t,o[0],o[1],r.internalFormatPackedFloat,t.RGBA,t.FLOAT)}function Ra(t,n,e){return Li(t,function(){return t.bindBuffer(t.ARRAY_BUFFER,e)}),ta(t,n,"clipSpacePos",e,3,20,0)&&ta(t,n,"uv",e,2,20,12)}function Ca(t,n,e){Li(t,function(){return t.bindTexture(t.TEXTURE_2D,n)}),Li(t,function(){return t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e)}),Li(t,function(){return t.bindTexture(t.TEXTURE_2D,null)})}function _a(t,n,e,r,o,i){Zi(t,e,r),Li(t,function(){return t.bindTexture(t.TEXTURE_2D,n)}),Li(t,function(){return t.texSubImage2D(t.TEXTURE_2D,0,0,0,e,r,i,t.FLOAT,o)}),Li(t,function(){return t.bindTexture(t.TEXTURE_2D,null)})}function ka(t,n,e,r,o,i,a){var u,s=ki(e,r),l=s[0],c=s[1];1===a.defaultNumChannels?u=o:function(t,n,e){var r=Ti(t.length,e);if(n.length= "+r);for(var o=0,i=0;i= "+o);for(var i=Di(n,e),a=i[0],u=i[1],s=e%2==1,l=n%2==1,c=Math.floor(e/2),f=Math.floor(n/2),h=s?4:0,p=e,d=0,v=0;v= "+o);for(var i=e%2==1,a=n%2==1,u=Math.floor(e/2),s=Math.floor(n/2),l=Di(n,e),c=l[0],f=l[1],h=i?4:0,p=e+(i?1:0),d=0,v=0,m=e,g=0;g>1;t[o]()?(r=o,n=o+1):e=o-1}return r}(this.itemsToPoll.map(function(t){return t.isDoneFn})),n=0;n<=t;++n)(0,this.itemsToPoll[n].resolveFn)();this.itemsToPoll=this.itemsToPoll.slice(t+1)},t.prototype.addItemToPoll=function(t,n){var e=this;this.itemsToPoll.push({isDoneFn:t,resolveFn:n}),this.itemsToPoll.length>1||S(function(){return e.pollItems(),0===e.itemsToPoll.length})},t.prototype.getQueryTime=function(t,n){if(0===n)return null;if(2===n){var e=this.gl;return e.getQueryParameter(t,e.QUERY_RESULT)/1e6}var r=this.getQueryTimerExtensionWebGL1();return r.getQueryObjectEXT(t,r.QUERY_RESULT_EXT)/1e6},t.prototype.downloadMatrixDriverSetup=function(t){this.throwIfDisposed(),ia(this.gl,t,this.framebuffer),this.autoDebugValidate&&ua(this.gl)},t.prototype.downloadMatrixDriverTeardown=function(){null!=this.outputTexture?(ia(this.gl,this.outputTexture,this.framebuffer),this.autoDebugValidate&&ua(this.gl)):aa(this.gl,this.framebuffer)},t.prototype.downloadMatrixDriver=function(t,n){this.downloadMatrixDriverSetup(t);var e=n();return this.downloadMatrixDriverTeardown(),e},t.prototype.downloadMatrixDriverAsync=function(t,n){return o(this,void 0,void 0,function(){var e;return i(this,function(r){switch(r.label){case 0:return this.downloadMatrixDriverSetup(t),[4,n()];case 1:return e=r.sent(),this.downloadMatrixDriverTeardown(),[2,e]}})})},t.prototype.setOutputMatrixTextureDriver=function(t,n,e){this.throwIfDisposed();var r=this.gl;ia(r,t,this.framebuffer),this.autoDebugValidate&&ua(r),this.outputTexture=t,Li(r,function(){return r.viewport(0,0,n,e)}),Li(r,function(){return r.scissor(0,0,n,e)})},t.prototype.setOutputMatrixWriteRegionDriver=function(t,n,e,r){var o=this;this.throwIfDisposed(),Li(this.gl,function(){return o.gl.scissor(t,n,e,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 La(t,n){if(t.length!==n.length)throw Error("Binary was compiled with "+t.length+" inputs, but was executed with "+n.length+" inputs");t.forEach(function(t,e){var r=t.logicalShape,o=n[e],i=o.tensor.shape;if(!y(r,i))throw Error("Binary was compiled with different shapes than the current args. Shapes "+r+" and "+i+" must match");if(!t.isUniform||!o.isUniform){var a=t.texShape,u=o.isUniform?null:o.texData.texShape;if(!y(a,u))throw Error("Binary was compiled with different texture shapes than the current args. Shape "+a+" and "+u+" must match")}})}var Fa=function(t,n,e){var r,o;if(this.variableNames=["c","a","b"],this.outputShape=n,e>4)throw Error("Where for rank "+e+" is not yet supported");if(1===e)o="resRC",r="resRC";else{for(var i=["resRC.x","resRC.y","resRC.z","resRC.w"],a=[],u=[],s=0;s= 1.0) {\n setOutput(getA("+o+"));\n } else {\n setOutput(getB("+o+"));\n }\n }\n "},Ba=function(t,n,e,r,o){this.variableNames=["x"],this.outputShape=[];var i,a=n,u=t[3]-1;this.outputShape=t;var s="float("+e+") + float("+r+") * sum";i=.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 = -"+a+"; j <= "+a+"; 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 * "+i+";\n setOutput(val);\n }\n "},Pa=function(t){this.variableNames=["dy","maxPos"],this.outputShape=t.inShape;var n=t.filterHeight,e=t.filterWidth,r=t.strideHeight,o=t.strideWidth,i=n-1-t.padInfo.top,a=e-1-t.padInfo.left,u=n*e-1;this.userCode="\n const ivec2 pads = ivec2("+i+", "+a+");\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 < "+n+"; 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 for (int wC = 0; wC < "+e+"; 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 float dyValue = getDy(b, idyR, idyC, d);\n int maxPosValue = "+u+" - int(getMaxPos(b, idyR, idyC, d));\n\n // Get the current value, check it against the value from the\n // position matrix.\n int curPosValue = wR * "+e+" + wC;\n float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);\n\n dotProd += dyValue * mask;\n }\n }\n setOutput(dotProd);\n }\n "},Ua=function(t,n,e,r){void 0===e&&(e=!1),void 0===r&&(r=!1),this.variableNames=["matrixA","matrixB"];var o=e?t[1]:t[0],i=r?n[0]:n[1],a=e?t[0]:t[1];this.outputShape=[o,i];var u=function(t,n){return e?n+" + "+t+", aRow":"aRow, "+n+" + "+t},s=function(t,n){return r?"bCol, "+n+" + "+t:n+" + "+t+", bCol"},l=4*Math.floor(a/4),c=a%4;this.userCode=" float dotARowBCol(int aRow, int bCol) {\n float result = 0.0;\n for (int i = 0; i < "+l+"; i += 4) {\n vec4 a = vec4(\n getMatrixA("+u(0,"i")+"),\n getMatrixA("+u(1,"i")+"),\n getMatrixA("+u(2,"i")+"),\n getMatrixA("+u(3,"i")+")\n );\n vec4 b = vec4(\n getMatrixB("+s(0,"i")+"),\n getMatrixB("+s(1,"i")+"),\n getMatrixB("+s(2,"i")+"),\n getMatrixB("+s(3,"i")+")\n );\n\n result += dot(a, b);\n }\n\n if ("+(1===c)+") {\n result += getMatrixA("+u(0,l)+") *\n getMatrixB("+s(0,l)+");\n } else if ("+(2===c)+") {\n vec2 a = vec2(\n getMatrixA("+u(0,l)+"),\n getMatrixA("+u(1,l)+")\n );\n vec2 b = vec2(\n getMatrixB("+s(0,l)+"),\n getMatrixB("+s(1,l)+")\n );\n result += dot(a, b);\n } else if ("+(3===c)+") {\n vec3 a = vec3(\n getMatrixA("+u(0,l)+"),\n getMatrixA("+u(1,l)+"),\n getMatrixA("+u(2,l)+")\n );\n vec3 b = vec3(\n getMatrixB("+s(0,l)+"),\n getMatrixB("+s(1,l)+"),\n getMatrixB("+s(2,l)+")\n );\n result += dot(a, b);\n }\n\n return result;\n }\n\n void main() {\n ivec2 resRC = getOutputCoords();\n setOutput(dotARowBCol(resRC.x, resRC.y));\n }\n "},Wa=function(){function t(t,n,e){this.variableNames=["probs"],this.outputShape=[t,e],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 < "+(n-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("+(n-1)+"));\n }\n "}return t.prototype.getCustomSetupFunc=function(t){var n=this;return function(e,r){null==n.seedLoc&&(n.seedLoc=e.getUniformLocation(r,"seed")),e.gl.uniform1f(n.seedLoc,t)}},t}(),za=function(t,n,e,r){this.variableNames=["indices"],this.outputShape=[t,n],this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int index = round(getIndices(coords.x));\n setOutput(mix(float("+r+"), float("+e+"),\n float(index == coords.y)));\n }\n "},Va=function(t,n,e){this.variableNames=["x"],this.outputShape=n.map(function(n,e){return n[0]+t[e]+n[1]});var r=t.length,o=gi(r),i=n.map(function(t){return t[0]}).join(","),a=n.map(function(n,e){return n[0]+t[e]}).join(","),u=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,r);this.userCode=1!==r?"\n "+o+" start = "+o+"("+i+");\n "+o+" end = "+o+"("+a+");\n\n void main() {\n "+o+" outC = getOutputCoords();\n if (any(lessThan(outC, start)) || any(greaterThanEqual(outC, end))) {\n setOutput(float("+e+"));\n } else {\n "+o+" coords = outC - start;\n setOutput(getX("+u+"));\n }\n }\n ":"\n int start = "+i+";\n int end = "+a+";\n\n void main() {\n int outC = getOutputCoords();\n if (outC < start || outC >= end) {\n setOutput(float("+e+"));\n } else {\n setOutput(getX(outC - start));\n }\n }\n "},Ga=function(t,n,e){if(this.variableNames=["x"],"avg"===n&&e)throw new Error("Cannot compute positions for average pool.");var r=t.filterHeight,o=t.filterWidth,i=t.strideHeight,a=t.strideWidth,u=t.padInfo.top,s=t.padInfo.left;this.outputShape=t.outShape;var l="avg"===n,c="0.0";if(l||(c="-1.0 / 0.0"),e)this.userCode="\n const ivec2 strides = ivec2("+i+", "+a+");\n const ivec2 pads = ivec2("+u+", "+s+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n float minMaxValue = 0.0;\n float minMaxValueFound = 0.0;\n int minMaxPosition = 0;\n float avgValue = 0.0;\n\n for (int wR = 0; wR < "+r+"; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+o+"; wC++) {\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 * "+o+" + wC;\n }\n }\n }\n setOutput(float(minMaxPosition));\n }\n ";else{var f=n+"("+n+"("+n+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"avg"===n&&(f="avgValue / count");var h=4*Math.floor(o/4),p=o%4,d="\n if ("+l+") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = max(values, minMaxValue);\n }\n ";this.userCode="\n const ivec2 strides = ivec2("+i+", "+a+");\n const ivec2 pads = ivec2("+u+", "+s+");\n const float initializationValue = "+c+";\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("+c+");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wR = 0; wR < "+r+"; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+h+"; wC += 4) {\n int xC = xCCorner + wC;\n\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n getValue(batch, xR, xC + 3, d)\n );\n\n "+d+"\n }\n\n int xC = xCCorner + "+h+";\n if ("+(1===p)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+d+"\n } else if ("+(2===p)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n initializationValue,\n initializationValue\n );\n\n "+d+"\n } else if ("+(3===p)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n initializationValue\n );\n\n "+d+"\n }\n }\n setOutput("+f+");\n }\n "}},qa=function(t,n){this.variableNames=["x"];var e=t.windowSize,r=t.batchSize,o=t.inSize,i=Math.ceil(o/e);this.outputShape=[r,i];var a="0.0",u="";"min"===n?(a="1.0 / 0.0",u="min"):"max"===n&&(a="-1.0 / 0.0",u="max");var s=n+"("+n+"("+n+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"sum"===n?s="sumValue":"all"===n&&(s="allValue");var l=4*Math.floor(e/4),c=e%4,f="\n if ("+("sum"===n)+") {\n sumValue += dot(values, ones);\n } else {\n minMaxValue = "+u+"(values, minMaxValue);\n }\n ",h="vec4";"all"===n&&(a="1.0",f="\n bool reducedAllValue = all(values);\n float floatedReducedAllValue = float(reducedAllValue);\n allValue = float(allValue >= 1.0 && floatedReducedAllValue >= 1.0);\n ",h="bvec4");var p="";o%e>0&&(p="\n if (inIdx < 0 || inIdx >= "+o+") {\n return initializationValue;\n }\n "),this.userCode="\n const float initializationValue = "+a+";\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 * "+e+";\n\n vec4 minMaxValue = vec4("+a+");\n float sumValue = 0.0;\n float allValue = 1.0;\n\n for (int i = 0; i < "+l+"; i += 4) {\n int inIdx = inOffset + i;\n "+h+" values = "+h+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n "+f+"\n }\n\n int inIdx = inOffset + "+l+";\n if ("+(1===c)+") {\n "+h+" values = "+h+"(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(2===c)+") {\n "+h+" values = "+h+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(3===c)+") {\n "+h+" values = "+h+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n "+f+"\n }\n setOutput("+s+");\n }\n "},Ha=function(t,n,e){this.variableNames=["dy"],this.outputShape=[],this.outputShape=n.shape;var r=n.shape,o=r[1],i=r[2],a=t.shape,u=a[1],s=a[2],l=[e&&u>1?o-1:o,e&&s>1?i-1:i],c=[e&&u>1?u-1:u,e&&s>1?s-1:s],f=l[0]/c[0],h=l[1]/c[1],p=1/f,d=1/h,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("+f+");\n const float widthScale = float("+h+");\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), "+(i-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 "},ja=function(t,n,e,r){this.variableNames=["A"],this.outputShape=[];var o=t[0],i=t[1],a=t[2],u=t[3];this.outputShape=[o,n,e,u];var s=[r&&n>1?i-1:i,r&&e>1?a-1:a],l=[r&&n>1?n-1:n,r&&e>1?e-1:e];this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+s[0]/l[0]+",\n "+s[1]/l[1]+");\n const vec2 inputShapeRC = vec2("+i+".0, "+a+".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 "},Ka=function(t,n,e,r){this.variableNames=["A"],this.outputShape=[];var o=t[0],i=t[1],a=t[2],u=t[3];this.outputShape=[o,n,e,u];var s=r?[i-1,a-1]:[i,a],l=r?[n-1,e-1]:[n,e],c=r?"0.5":"0.0";this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+s[0]/l[0]+",\n "+s[1]/l[1]+");\n const vec2 inputShapeRC = vec2("+i+".0, "+a+".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 + "+c+")));\n\n float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);\n\n setOutput(newValue);\n }\n "},Xa=function(t,n){this.variableNames=["x"];var e=t.length;if(e>4)throw new Error("WebGL backend: Reverse of rank-"+e+" tensor is not yet supported");if(this.outputShape=t,1!==e){var r=t.map(function(e,r){return function(e){return-1!==n.indexOf(e)&&1!==t[e]?t[e]+" - coords["+e+"] - 1":"coords["+e+"]"}(r)}).join(","),o=gi(e);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 "},Qa=function(t,n){this.variableNames=["x","segmentIds"];var e=t.windowSize,r=t.batchSize,o=t.inSize,i=t.numSegments,a=i*Math.ceil(o/e);this.outputShape=[r,a];var u=4*Math.floor(e/4),s=e%4,l="\n sumValue += dot(values, filter);\n ",c="";o%e>0&&(c="\n if (inIdx < 0 || inIdx >= "+o+") {\n return initializationValue;\n }\n ");var f="";o%e>0&&(f="\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 "+c+"\n return getX(batch, inIdx);\n }\n\n float getSegmentIdAtIndex(int inIdx) {\n "+f+"\n return getSegmentIds(inIdx);\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = int(floor(float(outIdx) / float(\n "+i+")) * float("+e+"));\n int currentSeg = int(mod(float(outIdx), float("+i+")));\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 filter = 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 "+l+"\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 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n 0,\n 0,\n 0\n );\n\n "+l+"\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 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n 0,\n 0\n );\n\n "+l+"\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 filter = 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 "+l+"\n }\n setOutput(sumValue);\n }\n "},Ya=function(){function t(t){this.variableNames=["source"],this.outputShape=t,this.rank=t.length;var n=gi(this.rank),e=function(t){if(1===t)return"sourceLoc";if(2===t)return"sourceLoc.x, sourceLoc.y";if(3===t)return"sourceLoc.x, sourceLoc.y, sourceLoc.z";if(4===t)return"sourceLoc.x, sourceLoc.y, sourceLoc.z, sourceLoc.w";throw Error("Slicing for rank "+t+" is not yet supported")}(this.rank);this.userCode="\n uniform "+n+" start;\n\n void main() {\n "+n+" sourceLoc = start + getOutputCoords();\n setOutput(getSource("+e+"));\n }\n "}return t.prototype.getCustomSetupFunc=function(t){var n=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(e,r){if(null!=n.startLoc||(n.startLoc=e.getUniformLocationNoThrow(r,"start"),null!=n.startLoc))if(1===n.rank)e.gl.uniform1i(n.startLoc,t[0]);else if(2===n.rank)e.gl.uniform2i(n.startLoc,t[0],t[1]);else if(3===n.rank)e.gl.uniform3i(n.startLoc,t[0],t[1],t[2]);else{if(4!==n.rank)throw Error("Slicing for rank "+n.rank+" is not yet supported");e.gl.uniform4i(n.startLoc,t[0],t[1],t[2],t[3])}}},t}();var Ja=function(t,n,e){this.variableNames=["x"],this.outputShape=e,this.rank=e.length;var r,o=gi(this.rank);r=1===this.rank?"coords * strides + begin":e.map(function(t,n){return"coords["+n+"] * strides["+n+"] + begin["+n+"]"}).join(","),this.userCode="\n "+o+" begin = "+o+"("+t+");\n "+o+" strides = "+o+"("+n+");\n\n void main() {\n "+o+" coords = getOutputCoords();\n setOutput(getX("+r+"));\n }\n "},Za=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,n){var e,r=$a(n),o=tu(t,r);if(o in this.freeTextures||(this.freeTextures[o]=[]),o in this.usedTextures||(this.usedTextures[o]=[]),this.freeTextures[o].length>0){this.numFreeTextures--,this.numUsedTextures++,this.log();var i=this.freeTextures[o].shift();return this.usedTextures[o].push(i),i}return this.numUsedTextures++,this.log(),r===Si.FLOAT32?e=this.gpgpu.createFloat32MatrixTexture(t[0],t[1]):r===Si.FLOAT16?e=this.gpgpu.createFloat16MatrixTexture(t[0],t[1]):r===Si.UNSIGNED_BYTE&&(e=this.gpgpu.createUnsignedBytesMatrixTexture(t[0],t[1])),this.usedTextures[o].push(e),e},t.prototype.releaseTexture=function(t,n,e){var r=tu(n,$a(e));r in this.freeTextures||(this.freeTextures[r]=[]),this.freeTextures[r].push(t),this.numFreeTextures++,this.numUsedTextures--;var o=this.usedTextures[r],i=o.indexOf(t);if(i<0)throw new Error("Cannot release a texture that was never provided by this texture manager");o.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 n in this.freeTextures)this.freeTextures[n].forEach(function(n){t.gpgpu.deleteMatrixTexture(n)});for(var n in this.usedTextures)this.usedTextures[n].forEach(function(n){t.gpgpu.deleteMatrixTexture(n)});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0}},t}();function $a(t){if(t===Ei.DOWNLOAD||t===Ei.PIXELS)return Si.UNSIGNED_BYTE;if(t===Ei.UPLOAD)return Si.FLOAT32;if(t===Ei.RENDER)return Xo.get("WEBGL_RENDER_FLOAT32_ENABLED")?Si.FLOAT32:Si.FLOAT16;throw new Error("Unknown logical texture type "+t)}function tu(t,n){return t[0]+"_"+t[1]+"_"+n}var nu=function(t,n){this.variableNames=["A"];for(var e=new Array(t.length),r=0;r5)throw Error("Tile for rank "+n+" is not yet supported");if(1===n)return"imod(resRC, "+t[0]+")";for(var e=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u"],r=[],o=0;o6)throw Error("Transpose for rank "+n+" is not yet supported");for(var e=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u","resRC.v"],r=new Array(n),o=0;o0?this.gpgpu.beginQuery():{startMs:performance.now(),endMs:null}},t.prototype.endTimer=function(t){return Xo.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?(this.gpgpu.endQuery(),t):(t.endMs=performance.now(),t)},t.prototype.getQueryTime=function(t){return o(this,void 0,void 0,function(){var n;return i(this,function(e){return Xo.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?[2,this.gpgpu.pollQueryTime(t)]:[2,(n=t).endMs-n.startMs]})})},t.prototype.disposeData=function(t){if(!this.pendingDisposal.has(t))if(this.pendingRead.has(t))this.pendingDisposal.add(t);else if(this.texData.has(t)){var n=this.texData.get(t),e=n.texture,r=n.texShape,o=n.usage;null!=e&&this.releaseTexture(t,e,r,o),this.texData.delete(t)}},t.prototype.getTexture=function(t){return this.uploadToGPU(t),this.texData.get(t).texture},t.prototype.getGPGPUContext=function(){return this.gpgpu},t.prototype.getCanvas=function(){return this.canvas},t.prototype.slice=function(t,n,e){var r=new Ya(e),o=r.getCustomSetupFunc(n);return this.compileAndRun(r,[t],null,o)},t.prototype.stridedSlice=function(t,n,e,r,o,i){var a=nn(t.shape,n,e,r,o,i),u=a[0],s=a[1];if(s.some(function(t){return 0===t}))return Gr([],s);var l=new Ja(u,r,s);return this.compileAndRun(l,[t])},t.prototype.reverse=function(t,n){var e=new Xa(t.shape,n);return this.compileAndRun(e,[t])},t.prototype.concat=function(t,n){var e=new ii(t.shape,n.shape);return this.compileAndRun(e,[t,n])},t.prototype.neg=function(t){var n=new ru(t.shape,"return -x;");return this.compileAndRun(n,[t])},t.prototype.matMul=function(t,n,e,r){var o=new Ua(t.shape,n.shape,e,r);return this.compileAndRun(o,[t,n])},t.prototype.multiply=function(t,n){var e=new ri("return a * b;",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,yt(t.dtype,n.dtype));return this.compileAndRun(e,[t,n],r)},t.prototype.batchNormalization=function(t,n,e,r,o,i){var a=[t,n,e],u=null;null!=i&&(u=i.shape,a.push(i));var s=null;null!=o&&(s=o.shape,a.push(o));var l=new ei(t.shape,n.shape,e.shape,u,s,r);return this.compileAndRun(l,a)},t.prototype.localResponseNormalization4D=function(t,n,e,r,o){var i=new Ba(t.shape,n,e,r,o);return this.compileAndRun(i,[t])},t.prototype.tile=function(t,n){var e=new nu(t.shape,n);return this.compileAndRun(e,[t])},t.prototype.pad=function(t,n,e){var r=new Va(t.shape,n,e);return this.compileAndRun(r,[t])},t.prototype.transpose=function(t,n){var e=new eu(t.shape,n);return this.compileAndRun(e,[t])},t.prototype.gather=function(t,n,e){var r=new _i(t.shape,n.size,e);return this.compileAndRun(r,[t,n])},t.prototype.reduce=function(t,n,e){var r=t.shape[0],o=t.shape[1],i=Jo(o),a=new qa({windowSize:i,inSize:o,batchSize:r},n),u=a.outputShape,s=u[0],l=u[1],c=this.makeOutputArray([s,l],e);return this.compileAndRun(a,[t],c),1===c.shape[1]?c:this.reduce(c,n,e)},t.prototype.argReduce=function(t,n,e){void 0===e&&(e=null);var r=t.shape[0],o=t.shape[1];null!=e&&(r=e.shape[0],o=e.shape[1]);var i=Jo(o),a=new ti({windowSize:i,inSize:o,batchSize:r},n,null==e),u=a.outputShape,s=u[0],l=u[1],c=this.makeOutputArray([s,l],"int32"),f=[t];return null!=e&&f.push(e),this.compileAndRun(a,f,c),1===c.shape[1]?c:this.argReduce(t,n,c)},t.prototype.sum=function(t,n){K("sum",n,t.rank);var e=q(t.shape,n),r=e[0],o=g(e[1]),i=t.as2D(-1,o),a=xt(t.dtype);return this.reduce(i,"sum",a).reshape(r)},t.prototype.unsortedSegmentSum=function(t,n,e){var r=0,o=X([r],t.rank),i=t;null!=o&&(i=t.transpose(o),r=Y(1,t.rank)[0]);var a=function(t,n,e){for(var r=[],o=t.length,i=0;in||e===t){r=!0;break}e=F(t,e+1)}return e}(a,o),s=new Qa({windowSize:u,inSize:a,batchSize:i,numSegments:o},n),l=s.outputShape,c=l[0],f=l[1],h=this.makeOutputArray([c,f],r);return this.compileAndRun(s,[t,e],h),h.shape[1]===o?h:(e=Wr(0,o).tile([a/u]),this.segOpCompute(h,n,e,r,o))},t.prototype.argMin=function(t,n){var e=[n];K("argMin",e,t.rank);var r=q(t.shape,e),o=r[0],i=g(r[1]),a=t.as2D(-1,i);return this.argReduce(a,"min").reshape(o)},t.prototype.argMax=function(t,n){var e=[n];K("argMax",e,t.rank);var r=q(t.shape,e),o=r[0],i=g(r[1]),a=t.as2D(-1,i);return this.argReduce(a,"max").reshape(o)},t.prototype.cumsum=function(t,n,e,r){if(n!==t.rank-1)throw new Error("WebGL cumsum shader expects an inner-most axis="+(t.rank-1)+" but got axis="+n);var o=new wi(t.shape,e,r);return this.compileAndRun(o,[t])},t.prototype.equal=function(t,n){var e=new ri("return float(a == b);",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t,n],r)},t.prototype.notEqual=function(t,n){var e=new ri("return float(a != b);",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t,n],r)},t.prototype.less=function(t,n){var e=new ri("return float(a < b);",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t,n],r)},t.prototype.lessEqual=function(t,n){var e=new ri("return float(a <= b);",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t,n],r)},t.prototype.greater=function(t,n){var e=new ri("return float(a > b);",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t,n],r)},t.prototype.greaterEqual=function(t,n){var e=new ri("return float(a >= b);",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t,n],r)},t.prototype.logicalNot=function(t){var n=new ru(t.shape,"return float(!(x >= 1.0));");return this.compileAndRun(n,[t])},t.prototype.logicalAnd=function(t,n){var e=new ri("return float(a >= 1.0 && b >= 1.0);",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t,n],r)},t.prototype.logicalOr=function(t,n){var e=new ri("return float(a >= 1.0 || b >= 1.0);",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t,n],r)},t.prototype.where=function(t,n,e,r){var o=new Fa(t.rank,n.shape,n.rank),i=this.makeOutputArray(o.outputShape,r);return this.compileAndRun(o,[t,n,e],i)},t.prototype.topKValues=function(t,n){throw new Error("topKValues GPU not yet implemented!")},t.prototype.topKIndices=function(t,n){throw new Error("topKIndices GPU not yet implemented!")},t.prototype.min=function(t,n){K("min",n,t.rank);var e=q(t.shape,n),r=e[0],o=g(e[1]),i=t.as2D(-1,o);return this.reduce(i,"min",i.dtype).reshape(r)},t.prototype.minimum=function(t,n){var e=new ri("\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n\n return min(a, b);\n",t.shape,n.shape);return this.compileAndRun(e,[t,n])},t.prototype.mod=function(t,n){var e=new ri("return mod(a, b);",t.shape,n.shape);return this.compileAndRun(e,[t,n])},t.prototype.max=function(t,n){K("max",n,t.rank);var e=q(t.shape,n),r=e[0],o=g(e[1]),i=t.as2D(-1,o);return this.reduce(i,"max",i.dtype).reshape(r)},t.prototype.maximum=function(t,n){var e=new ri("\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n\n return max(a, b);\n",t.shape,n.shape);return this.compileAndRun(e,[t,n])},t.prototype.all=function(t,n){K("all",n,t.rank);var e=q(t.shape,n),r=e[0],o=g(e[1]),i=t.as2D(-1,o);return this.reduce(i,"all",i.dtype).reshape(r)},t.prototype.squaredDifference=function(t,n){var e=new ri("return (a - b) * (a - b);",t.shape,n.shape);return this.compileAndRun(e,[t,n])},t.prototype.realDivide=function(t,n){var e=new ri("return a / b;",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,"float32");return this.compileAndRun(e,[t,n],r)},t.prototype.floorDiv=function(t,n){var e=new ri("\n float resultSign = sign(a) * sign(b);\n int ia = round(a);\n int ib = round(b);\n int result = ia / ib;\n int amodb = ia - ib * result;\n\n if (resultSign < 0.0 && amodb != 0) {\n result -= 1;\n }\n return float(result);\n",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,"int32");return this.compileAndRun(e,[t,n],r)},t.prototype.add=function(t,n){var e=new ri("return a + b;",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,yt(t.dtype,n.dtype));return this.compileAndRun(e,[t,n],r)},t.prototype.subtract=function(t,n){var e=new ri("return a - b;",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,yt(t.dtype,n.dtype));return this.compileAndRun(e,[t,n],r)},t.prototype.pow=function(t,n){var e=new ri("\n return (round(mod(b, 2.0)) == 0 || round(mod(b, 2.0)) == 2) ?\n pow(abs(a), b) : sign(a) * pow(abs(a), b);\n",t.shape,n.shape),r=this.makeOutputArray(e.outputShape,yt(t.dtype,n.dtype));return this.compileAndRun(e,[t,n],r)},t.prototype.ceil=function(t){var n=new ru(t.shape,"return ceil(x);");return this.compileAndRun(n,[t])},t.prototype.floor=function(t){var n=new ru(t.shape,"return floor(x);");return this.compileAndRun(n,[t])},t.prototype.sign=function(t){var n=new ru(t.shape,"\n if (isNaN(x)) { return 0.0; }\n return sign(x);\n");return this.compileAndRun(n,[t])},t.prototype.round=function(t){var n=new ru(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(n,[t])},t.prototype.exp=function(t){var n=new ru(t.shape,"return exp(x);");return this.compileAndRun(n,[t])},t.prototype.expm1=function(t){var n=new ru(t.shape,"return exp(x) - 1.0;");return this.compileAndRun(n,[t])},t.prototype.log=function(t){var n=new ru(t.shape,"return log(x);");return this.compileAndRun(n,[t])},t.prototype.log1p=function(t){var n=new ru(t.shape,"return log(1.0 + x);");return this.compileAndRun(n,[t])},t.prototype.sqrt=function(t){var n=new ru(t.shape,"return sqrt(x);");return this.compileAndRun(n,[t])},t.prototype.rsqrt=function(t){var n=new ru(t.shape,"return inversesqrt(x);");return this.compileAndRun(n,[t])},t.prototype.square=function(t){var n=new ru(t.shape,"return x * x;");return this.compileAndRun(n,[t])},t.prototype.reciprocal=function(t){var n=new ru(t.shape,"return 1.0 / x;");return this.compileAndRun(n,[t])},t.prototype.relu=function(t){var n=new ru(t.shape,iu);return this.compileAndRun(n,[t])},t.prototype.elu=function(t){var n=new ru(t.shape,"return (x >= 0.0) ? x : (exp(x) - 1.0);");return this.compileAndRun(n,[t])},t.prototype.eluDer=function(t,n){var e=new ri("return (b >= 1.0) ? a : a * (b + 1.0);",t.shape,n.shape);return this.compileAndRun(e,[t,n])},t.prototype.selu=function(t){var n=new ru(t.shape,"\n // Stable and Attracting Fixed Point (0, 1) for Normalized Weights.\n // see: https://arxiv.org/abs/1706.02515\n float scaleAlpha = 1.7580993408473768;\n float scale = 1.0507009873554805;\n return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);\n");return this.compileAndRun(n,[t])},t.prototype.int=function(t){var n=new ru(t.shape,"return float(int(x));"),e=this.makeOutputArray(n.outputShape,"int32");return this.compileAndRun(n,[t],e)},t.prototype.clip=function(t,n,e){var r=new oi(t.shape,n,e);return this.compileAndRun(r,[t])},t.prototype.abs=function(t){var n=new ru(t.shape,"return abs(x);");return this.compileAndRun(n,[t])},t.prototype.sigmoid=function(t){var n=new ru(t.shape,"return 1.0 / (1.0 + exp(-1.0 * x));");return this.compileAndRun(n,[t])},t.prototype.softplus=function(t){var n=new ru(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(n,[t])},t.prototype.sin=function(t){var n=new ru(t.shape,au);return this.compileAndRun(n,[t])},t.prototype.cos=function(t){var n=new ru(t.shape,uu);return this.compileAndRun(n,[t])},t.prototype.tan=function(t){var n=new ru(t.shape,"return tan(x);");return this.compileAndRun(n,[t])},t.prototype.asin=function(t){var n=new ru(t.shape,"return asin(x);");return this.compileAndRun(n,[t])},t.prototype.acos=function(t){var n=new ru(t.shape,"return acos(x);");return this.compileAndRun(n,[t])},t.prototype.atan=function(t){var n=new ru(t.shape,su);return this.compileAndRun(n,[t])},t.prototype.atan2=function(t,n){var e=new ri("\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n\n return atan(a, b);\n",t.shape,n.shape);return this.compileAndRun(e,[t,n])},t.prototype.sinh=function(t){var n=new ru(t.shape,"\n float e2x = exp(x);\n return (e2x - 1.0 / e2x) / 2.0;\n");return this.compileAndRun(n,[t])},t.prototype.cosh=function(t){var n=new ru(t.shape,"\n float e2x = exp(-x);\n return (e2x + 1.0 / e2x) / 2.0;\n");return this.compileAndRun(n,[t])},t.prototype.tanh=function(t){var n=new ru(t.shape,"\n float e2x = exp(-2.0 * abs(x));\n return sign(x) * (1.0 - e2x) / (1.0 + e2x);\n");return this.compileAndRun(n,[t])},t.prototype.asinh=function(t){var n=new ru(t.shape,"return log(x + sqrt(x * x + 1.0));");return this.compileAndRun(n,[t])},t.prototype.acosh=function(t){var n=new ru(t.shape,"return log(x + sqrt(x * x - 1.0));");return this.compileAndRun(n,[t])},t.prototype.atanh=function(t){var n=new ru(t.shape,"return (log(1.0 + x) - log(1.0 - x)) / 2.0;");return this.compileAndRun(n,[t])},t.prototype.erf=function(t){var n=new ru(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 t = 1.0 / (1.0 + p * x);\n return 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);\n');return this.compileAndRun(n,[t])},t.prototype.step=function(t,n){var e=new ru(t.shape,function(t){return void 0===t&&(t=0),ou+"\n return x > 0.0 ? 1.0 : float("+t+");\n "}(n));return this.compileAndRun(e,[t])},t.prototype.conv2d=function(t,n,e){var r=new ci(e);return this.compileAndRun(r,[t,n])},t.prototype.conv2dDerInput=function(t,n,e){var r=new ui(e);return this.compileAndRun(r,[t,n])},t.prototype.conv2dDerFilter=function(t,n,e){var r=new ai(e);return this.compileAndRun(r,[t,n])},t.prototype.depthwiseConv2D=function(t,n,e){var r=new fi(e);return this.compileAndRun(r,[t,n])},t.prototype.depthwiseConv2DDerInput=function(t,n,e){var r=new li(e);return this.compileAndRun(r,[t,n])},t.prototype.depthwiseConv2DDerFilter=function(t,n,e){var r=new si(e);return this.compileAndRun(r,[t,n])},t.prototype.maxPool=function(t,n){var e=new Ga(n,"max",!1),r=this.makeOutputArray(e.outputShape,t.dtype);return this.compileAndRun(e,[t],r)},t.prototype.avgPool=function(t,n){var e=new Ga(n,"avg",!1),r=this.makeOutputArray(e.outputShape,"float32");return this.compileAndRun(e,[t],r)},t.prototype.maxPoolBackprop=function(t,n,e,r){var o=new Ga(r,"max",!0),i=this.compileAndRun(o,[n]),a=new Pa(r),u=this.makeOutputArray(a.outputShape,n.dtype),s=this.compileAndRun(a,[t,i],u);return i.dispose(),s},t.prototype.avgPoolBackprop=function(t,n,e){var r=new ni(e),o=this.makeOutputArray(r.outputShape,n.dtype);return this.compileAndRun(r,[t],o)},t.prototype.cast=function(t,n){return Zo(t,n,this)},t.prototype.reshape=function(t,n){return $o(t,n)},t.prototype.resizeBilinear=function(t,n,e,r){var o=new ja(t.shape,n,e,r);return this.compileAndRun(o,[t])},t.prototype.resizeBilinearBackprop=function(t,n,e){var r=new Ha(t,n,e);return this.compileAndRun(r,[t])},t.prototype.resizeNearestNeighbor=function(t,n,e,r){var o=new Ka(t.shape,n,e,r);return this.compileAndRun(o,[t])},t.prototype.multinomial=function(t,n,e,r){var o=n?t:fo(t),i=o.shape[0],a=o.shape[1],u=new Wa(i,a,e),s=this.makeOutputArray(u.outputShape,"int32"),l=u.getCustomSetupFunc(r);return this.compileAndRun(u,[o],s,l)},t.prototype.oneHot=function(t,n,e,r){var o=new za(t.size,n,e,r);return this.compileAndRun(o,[t])},t.prototype.makeOutputArray=function(t,n){return xo.make(t,{},n)},t.prototype.compileAndRun=function(t,n,e,r){var o=this;null==e&&(e=this.makeOutputArray(t.outputShape,n[0].dtype));var i=n.map(function(t){var n=o.texData.get(t.dataId);return null==n.texture&&t.size<=32?{tensor:t,texData:null,isUniform:!0}:(o.uploadToGPU(t.dataId),{tensor:t,texData:n,isUniform:!1})});this.uploadToGPU(e.dataId);var a,u={tensor:e,texData:this.texData.get(e.dataId),isUniform:!1},s=function(t,n,e){var r="";n.concat(e).forEach(function(t){r+=t.tensor.shape+"_"+(t.isUniform?"uniform":t.texData.texShape)});var o=t.userCode,i=(!0===t.supportsBroadcasting).toString();return t.constructor.name+"_"+i+"_"+r+"_"+o}(t,i,u),l=this.getAndSaveBinary(s,function(){return function(t,n,e,r){for(var o=n.userCode,i=e.map(function(t,e){var r={logicalShape:t.tensor.shape,texShape:t.isUniform?null:t.texData.texShape,isUniform:t.isUniform};return{name:n.variableNames[e],shapeInfo:r}}),a=i.map(function(t){return t.shapeInfo}),u={logicalShape:r.tensor.shape,texShape:r.texData.texShape,isUniform:!1},s=hi(i,u,o,!0===n.supportsBroadcasting),l=t.createProgram(s),c={},f=0;fthis.NUM_BYTES_BEFORE_PAGING)for(var f=this.numBytesInGPU-this.NUM_BYTES_BEFORE_PAGING;f>0;){var h=this.lruDataGPU.shift(),p=this.texData.get(h),d=p.shape,v=p.dtype;f-=this.computeBytes(d,v),this.read(h)}return c&&(a=this.endTimer(a),this.activeTimers.push(this.getQueryTime(a))),e},t.prototype.getAndSaveBinary=function(t,n){return t in this.binaryCache||(this.binaryCache[t]=n()),this.binaryCache[t]},t.prototype.getTextureManager=function(){return this.textureManager},t.prototype.dispose=function(){if(!this.disposed){for(var t in this.binaryCache)this.gpgpu.deleteProgram(this.binaryCache[t].webGLProgram);this.textureManager.dispose(),this.canvas.remove(),null!=this.fromPixelsCanvas&&this.fromPixelsCanvas.remove(),this.gpgpuCreatedLocally&&this.gpgpu.dispose(),this.disposed=!0}},t.prototype.throwIfNoData=function(t){if(!this.texData.has(t))throw new Error("WebGL backend: No data found for this tensor. Did you change your backend in the middle of the program? New backends can't use Tensors created with previous backends")},t.prototype.uploadToGPU=function(t){this.throwIfNoData(t);var n=this.texData.get(t),e=n.shape,r=n.values,o=n.texture,i=(n.dtype,n.usage);if(null!=o)return this.lruDataGPU.splice(this.lruDataGPU.indexOf(t),1),void this.lruDataGPU.push(t);var a,u=null!=this.activeTimers;u&&(a=performance.now());var s=fa(this.gpgpu.gl,e);n.texShape=s;var l=this.acquireTexture(t,s,i);n.texture=l,null!=r&&(this.gpgpu.uploadMatrixToTexture(l,s[0],s[1],function(t,n){return t instanceof Float32Array?t:new Float32Array(t)}(r)),n.values=null,u&&(this.uploadWaitMs+=performance.now()-a))},t.prototype.cacheOnCPU=function(t,n){var e=this.delayedStorage,r=this.texData.get(t),o=r.texture,i=r.texShape,a=r.dtype,u=r.usage;e&&null!=o&&(this.releaseTexture(t,o,i,u),r.texture=null,r.texShape=null),null!=n&&(r.values=function(t,n){if("float32"===n)return t;if("int32"===n||"bool"===n){for(var e="int32"===n?new Int32Array(t.length):new Uint8Array(t.length),r=0;r=0&&this.lruDataGPU.splice(u,1),this.numBytesInGPU-=this.computeBytes(i,a),this.textureManager.releaseTexture(n,e,r)},t.prototype.acquireTexture=function(t,n,e){var r=this.texData.get(t),o=r.shape,i=r.dtype;return this.lruDataGPU.push(t),this.numBytesInGPU+=this.computeBytes(o,i),this.textureManager.acquireTexture(n,e)},t.prototype.computeBytes=function(t,n){return g(t)*N(n)},t}();Xo.get("IS_BROWSER")&&Xo.registerBackend("webgl",function(){return new cu},2);var fu=function(){function t(){this.data=new WeakMap,this.firstUse=!0,Xo.get("IS_BROWSER")&&(this.canvas=document.createElement("canvas"))}return t.prototype.register=function(t,n,e){if(this.firstUse&&(this.firstUse=!1,Xo.get("IS_NODE")&&console.warn("\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")),this.data.has(t))throw new Error("Data buffer is already registered");this.data.set(t,null)},t.prototype.write=function(t,n){if(null==n)throw new Error("MathBackendCPU.write(): values can not be null");this.throwIfNoData(t),this.data.set(t,n)},t.prototype.fromPixels=function(t,n){if(null==t)throw new Error("MathBackendCPU.writePixels(): pixels can not be null");var e,r;if(t instanceof ImageData)e=t.data;else if(t instanceof HTMLCanvasElement)e=t.getContext("2d").getImageData(0,0,t.width,t.height).data;else{if(!(t instanceof HTMLImageElement||t instanceof HTMLVideoElement))throw new Error("pixels is of unknown type: "+t.constructor.name);if(null==this.canvas)throw new Error("Can't read pixels from HTMLImageElement outside the browser.");this.canvas.width=t.width,this.canvas.height=t.height,this.canvas.getContext("2d").drawImage(t,0,0,t.width,t.height),e=this.canvas.getContext("2d").getImageData(0,0,t.width,t.height).data}if(4===n)r=new Int32Array(e);else{var o=t.width*t.height;r=new Int32Array(o*n);for(var i=0;ih&&(h=v,p=d)}s[c]=p}return a},t.prototype.cumsum=function(t,n,e,r){if(n!==t.rank-1)throw new Error("backend.cumsum in CPU expects an inner-most axis="+(t.rank-1)+" but got axis="+n);for(var o=yt(t.dtype,"int32"),i=_r(t.shape,o),a=i.dataSync(),u=t.dataSync(),s=t.shape[t.rank-1],l=r?function(t,n){return t+s-n-1}:function(t,n){return t+n},c=0;cn?1:0})},t.prototype.greaterEqual=function(t,n){return this.broadcastedBinaryOp(t,n,"bool",function(t,n){return t>=n?1:0})},t.prototype.logicalNot=function(t){for(var n=t.dataSync(),e=new Int32Array(n.length),r=0;r1||1===n.rank?1:n.shape[1],f=0;f=0&&n>=0?e:(e+n)%n})},t.prototype.max=function(t,n){K("max",n,t.rank);for(var e=q(t.shape,n),r=e[0],o=e[1],i=_r(r,t.dtype),a=g(o),u=i.dataSync(),s=t.dataSync(),l=0;lf&&(f=p)}u[l]=f}return i},t.prototype.maximum=function(t,n){return this.broadcastedBinaryOp(t,n,t.dtype,function(t,n){return Math.max(t,n)})},t.prototype.all=function(t,n){K("all",n,t.rank);for(var e=q(t.shape,n),r=e[0],o=e[1],i=_r(r,t.dtype),a=g(o),u=i.dataSync(),s=t.dataSync(),l=0;l0?e[r]=1:e[r]=0;return xo.make(t.shape,{values:e})},t.prototype.round=function(t){for(var n=t.dataSync(),e=new Float32Array(n.length),r=0;r.5?e[r]=Math.ceil(n[r]):e[r]=o%2==0?o:o+1}return xo.make(t.shape,{values:e})},t.prototype.exp=function(t){for(var n=t.dataSync(),e=new Float32Array(n.length),r=0;r=0?o:Math.exp(o)-1}return xo.make(t.shape,{values:n})},t.prototype.eluDer=function(t,n){for(var e=new Float32Array(n.size),r=n.dataSync(),o=t.dataSync(),i=0;i=1?o[i]:o[i]*(a+1)}return xo.make(n.shape,{values:e})},t.prototype.selu=function(t){for(var n=new Float32Array(t.size),e=t.dataSync(),r=0;r