var __create = Object.create; var __defProp = Object.defineProperty; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __markAsModule = (target) => __defProp(target, "__esModule", {value: true}); var __commonJS = (callback, module2) => () => { if (!module2) { module2 = {exports: {}}; callback(module2.exports, module2); } return module2.exports; }; var __export = (target, all) => { __markAsModule(target); for (var name in all) __defProp(target, name, {get: all[name], enumerable: true}); }; var __exportStar = (target, module2, desc) => { __markAsModule(target); if (typeof module2 === "object" || typeof module2 === "function") { for (let key of __getOwnPropNames(module2)) if (!__hasOwnProp.call(target, key) && key !== "default") __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable}); } return target; }; var __toModule = (module2) => { if (module2 && module2.__esModule) return module2; return __exportStar(__defProp(__create(__getProtoOf(module2)), "default", {value: module2, enumerable: true}), module2); }; // node_modules/node-fetch/lib/index.mjs var require_lib = __commonJS((exports2) => { __export(exports2, { FetchError: () => FetchError, Headers: () => Headers, Request: () => Request, Response: () => Response, default: () => lib_default }); const stream = __toModule(require("stream")); const http2 = __toModule(require("http")); const url = __toModule(require("url")); const https2 = __toModule(require("https")); const zlib2 = __toModule(require("zlib")); const Readable = stream.default.Readable; const BUFFER = Symbol("buffer"); const TYPE = Symbol("type"); class Blob2 { constructor() { this[TYPE] = ""; const blobParts = arguments[0]; const options = arguments[1]; const buffers = []; let size = 0; if (blobParts) { const a = blobParts; const length = Number(a.length); for (let i = 0; i < length; i++) { const element = a[i]; let buffer2; if (element instanceof Buffer) { buffer2 = element; } else if (ArrayBuffer.isView(element)) { buffer2 = Buffer.from(element.buffer, element.byteOffset, element.byteLength); } else if (element instanceof ArrayBuffer) { buffer2 = Buffer.from(element); } else if (element instanceof Blob2) { buffer2 = element[BUFFER]; } else { buffer2 = Buffer.from(typeof element === "string" ? element : String(element)); } size += buffer2.length; buffers.push(buffer2); } } this[BUFFER] = Buffer.concat(buffers); let type = options && options.type !== void 0 && String(options.type).toLowerCase(); if (type && !/[^\u0020-\u007E]/.test(type)) { this[TYPE] = type; } } get size() { return this[BUFFER].length; } get type() { return this[TYPE]; } text() { return Promise.resolve(this[BUFFER].toString()); } arrayBuffer() { const buf = this[BUFFER]; const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); return Promise.resolve(ab); } stream() { const readable = new Readable(); readable._read = function() { }; readable.push(this[BUFFER]); readable.push(null); return readable; } toString() { return "[object Blob]"; } slice() { const size = this.size; const start = arguments[0]; const end = arguments[1]; let relativeStart, relativeEnd; if (start === void 0) { relativeStart = 0; } else if (start < 0) { relativeStart = Math.max(size + start, 0); } else { relativeStart = Math.min(start, size); } if (end === void 0) { relativeEnd = size; } else if (end < 0) { relativeEnd = Math.max(size + end, 0); } else { relativeEnd = Math.min(end, size); } const span = Math.max(relativeEnd - relativeStart, 0); const buffer2 = this[BUFFER]; const slicedBuffer = buffer2.slice(relativeStart, relativeStart + span); const blob = new Blob2([], {type: arguments[2]}); blob[BUFFER] = slicedBuffer; return blob; } } Object.defineProperties(Blob2.prototype, { size: {enumerable: true}, type: {enumerable: true}, slice: {enumerable: true} }); Object.defineProperty(Blob2.prototype, Symbol.toStringTag, { value: "Blob", writable: false, enumerable: false, configurable: true }); function FetchError(message, type, systemError) { Error.call(this, message); this.message = message; this.type = type; if (systemError) { this.code = this.errno = systemError.code; } Error.captureStackTrace(this, this.constructor); } FetchError.prototype = Object.create(Error.prototype); FetchError.prototype.constructor = FetchError; FetchError.prototype.name = "FetchError"; let convert; try { convert = require("encoding").convert; } catch (e) { } const INTERNALS = Symbol("Body internals"); const PassThrough = stream.default.PassThrough; function Body(body2) { var _this = this; var _ref = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, _ref$size = _ref.size; let size = _ref$size === void 0 ? 0 : _ref$size; var _ref$timeout = _ref.timeout; let timeout = _ref$timeout === void 0 ? 0 : _ref$timeout; if (body2 == null) { body2 = null; } else if (isURLSearchParams(body2)) { body2 = Buffer.from(body2.toString()); } else if (isBlob(body2)) ; else if (Buffer.isBuffer(body2)) ; else if (Object.prototype.toString.call(body2) === "[object ArrayBuffer]") { body2 = Buffer.from(body2); } else if (ArrayBuffer.isView(body2)) { body2 = Buffer.from(body2.buffer, body2.byteOffset, body2.byteLength); } else if (body2 instanceof stream.default) ; else { body2 = Buffer.from(String(body2)); } this[INTERNALS] = { body: body2, disturbed: false, error: null }; this.size = size; this.timeout = timeout; if (body2 instanceof stream.default) { body2.on("error", function(err) { const error = err.name === "AbortError" ? err : new FetchError(`Invalid response body while trying to fetch ${_this.url}: ${err.message}`, "system", err); _this[INTERNALS].error = error; }); } } Body.prototype = { get body() { return this[INTERNALS].body; }, get bodyUsed() { return this[INTERNALS].disturbed; }, arrayBuffer() { return consumeBody.call(this).then(function(buf) { return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); }); }, blob() { let ct = this.headers && this.headers.get("content-type") || ""; return consumeBody.call(this).then(function(buf) { return Object.assign(new Blob2([], { type: ct.toLowerCase() }), { [BUFFER]: buf }); }); }, json() { var _this2 = this; return consumeBody.call(this).then(function(buffer2) { try { return JSON.parse(buffer2.toString()); } catch (err) { return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, "invalid-json")); } }); }, text() { return consumeBody.call(this).then(function(buffer2) { return buffer2.toString(); }); }, buffer() { return consumeBody.call(this); }, textConverted() { var _this3 = this; return consumeBody.call(this).then(function(buffer2) { return convertBody(buffer2, _this3.headers); }); } }; Object.defineProperties(Body.prototype, { body: {enumerable: true}, bodyUsed: {enumerable: true}, arrayBuffer: {enumerable: true}, blob: {enumerable: true}, json: {enumerable: true}, text: {enumerable: true} }); Body.mixIn = function(proto) { for (const name of Object.getOwnPropertyNames(Body.prototype)) { if (!(name in proto)) { const desc = Object.getOwnPropertyDescriptor(Body.prototype, name); Object.defineProperty(proto, name, desc); } } }; function consumeBody() { var _this4 = this; if (this[INTERNALS].disturbed) { return Body.Promise.reject(new TypeError(`body used already for: ${this.url}`)); } this[INTERNALS].disturbed = true; if (this[INTERNALS].error) { return Body.Promise.reject(this[INTERNALS].error); } let body2 = this.body; if (body2 === null) { return Body.Promise.resolve(Buffer.alloc(0)); } if (isBlob(body2)) { body2 = body2.stream(); } if (Buffer.isBuffer(body2)) { return Body.Promise.resolve(body2); } if (!(body2 instanceof stream.default)) { return Body.Promise.resolve(Buffer.alloc(0)); } let accum = []; let accumBytes = 0; let abort = false; return new Body.Promise(function(resolve, reject) { let resTimeout; if (_this4.timeout) { resTimeout = setTimeout(function() { abort = true; reject(new FetchError(`Response timeout while trying to fetch ${_this4.url} (over ${_this4.timeout}ms)`, "body-timeout")); }, _this4.timeout); } body2.on("error", function(err) { if (err.name === "AbortError") { abort = true; reject(err); } else { reject(new FetchError(`Invalid response body while trying to fetch ${_this4.url}: ${err.message}`, "system", err)); } }); body2.on("data", function(chunk) { if (abort || chunk === null) { return; } if (_this4.size && accumBytes + chunk.length > _this4.size) { abort = true; reject(new FetchError(`content size at ${_this4.url} over limit: ${_this4.size}`, "max-size")); return; } accumBytes += chunk.length; accum.push(chunk); }); body2.on("end", function() { if (abort) { return; } clearTimeout(resTimeout); try { resolve(Buffer.concat(accum, accumBytes)); } catch (err) { reject(new FetchError(`Could not create Buffer from response body for ${_this4.url}: ${err.message}`, "system", err)); } }); }); } function convertBody(buffer2, headers) { if (typeof convert !== "function") { throw new Error("The package `encoding` must be installed to use the textConverted() function"); } const ct = headers.get("content-type"); let charset = "utf-8"; let res, str; if (ct) { res = /charset=([^;]*)/i.exec(ct); } str = buffer2.slice(0, 1024).toString(); if (!res && str) { res = / 0 && arguments[0] !== void 0 ? arguments[0] : void 0; this[MAP] = Object.create(null); if (init2 instanceof Headers) { const rawHeaders = init2.raw(); const headerNames = Object.keys(rawHeaders); for (const headerName of headerNames) { for (const value of rawHeaders[headerName]) { this.append(headerName, value); } } return; } if (init2 == null) ; else if (typeof init2 === "object") { const method = init2[Symbol.iterator]; if (method != null) { if (typeof method !== "function") { throw new TypeError("Header pairs must be iterable"); } const pairs = []; for (const pair of init2) { if (typeof pair !== "object" || typeof pair[Symbol.iterator] !== "function") { throw new TypeError("Each header pair must be iterable"); } pairs.push(Array.from(pair)); } for (const pair of pairs) { if (pair.length !== 2) { throw new TypeError("Each header pair must be a name/value tuple"); } this.append(pair[0], pair[1]); } } else { for (const key of Object.keys(init2)) { const value = init2[key]; this.append(key, value); } } } else { throw new TypeError("Provided initializer must be an object"); } } get(name) { name = `${name}`; validateName(name); const key = find(this[MAP], name); if (key === void 0) { return null; } return this[MAP][key].join(", "); } forEach(callback) { let thisArg = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : void 0; let pairs = getHeaders(this); let i = 0; while (i < pairs.length) { var _pairs$i = pairs[i]; const name = _pairs$i[0], value = _pairs$i[1]; callback.call(thisArg, value, name, this); pairs = getHeaders(this); i++; } } set(name, value) { name = `${name}`; value = `${value}`; validateName(name); validateValue(value); const key = find(this[MAP], name); this[MAP][key !== void 0 ? key : name] = [value]; } append(name, value) { name = `${name}`; value = `${value}`; validateName(name); validateValue(value); const key = find(this[MAP], name); if (key !== void 0) { this[MAP][key].push(value); } else { this[MAP][name] = [value]; } } has(name) { name = `${name}`; validateName(name); return find(this[MAP], name) !== void 0; } delete(name) { name = `${name}`; validateName(name); const key = find(this[MAP], name); if (key !== void 0) { delete this[MAP][key]; } } raw() { return this[MAP]; } keys() { return createHeadersIterator(this, "key"); } values() { return createHeadersIterator(this, "value"); } [Symbol.iterator]() { return createHeadersIterator(this, "key+value"); } } Headers.prototype.entries = Headers.prototype[Symbol.iterator]; Object.defineProperty(Headers.prototype, Symbol.toStringTag, { value: "Headers", writable: false, enumerable: false, configurable: true }); Object.defineProperties(Headers.prototype, { get: {enumerable: true}, forEach: {enumerable: true}, set: {enumerable: true}, append: {enumerable: true}, has: {enumerable: true}, delete: {enumerable: true}, keys: {enumerable: true}, values: {enumerable: true}, entries: {enumerable: true} }); function getHeaders(headers) { let kind = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "key+value"; const keys = Object.keys(headers[MAP]).sort(); return keys.map(kind === "key" ? function(k) { return k.toLowerCase(); } : kind === "value" ? function(k) { return headers[MAP][k].join(", "); } : function(k) { return [k.toLowerCase(), headers[MAP][k].join(", ")]; }); } const INTERNAL = Symbol("internal"); function createHeadersIterator(target, kind) { const iterator = Object.create(HeadersIteratorPrototype); iterator[INTERNAL] = { target, kind, index: 0 }; return iterator; } const HeadersIteratorPrototype = Object.setPrototypeOf({ next() { if (!this || Object.getPrototypeOf(this) !== HeadersIteratorPrototype) { throw new TypeError("Value of `this` is not a HeadersIterator"); } var _INTERNAL = this[INTERNAL]; const target = _INTERNAL.target, kind = _INTERNAL.kind, index = _INTERNAL.index; const values = getHeaders(target, kind); const len = values.length; if (index >= len) { return { value: void 0, done: true }; } this[INTERNAL].index = index + 1; return { value: values[index], done: false }; } }, Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()))); Object.defineProperty(HeadersIteratorPrototype, Symbol.toStringTag, { value: "HeadersIterator", writable: false, enumerable: false, configurable: true }); function exportNodeCompatibleHeaders(headers) { const obj = Object.assign({__proto__: null}, headers[MAP]); const hostHeaderKey = find(headers[MAP], "Host"); if (hostHeaderKey !== void 0) { obj[hostHeaderKey] = obj[hostHeaderKey][0]; } return obj; } function createHeadersLenient(obj) { const headers = new Headers(); for (const name of Object.keys(obj)) { if (invalidTokenRegex.test(name)) { continue; } if (Array.isArray(obj[name])) { for (const val of obj[name]) { if (invalidHeaderCharRegex.test(val)) { continue; } if (headers[MAP][name] === void 0) { headers[MAP][name] = [val]; } else { headers[MAP][name].push(val); } } } else if (!invalidHeaderCharRegex.test(obj[name])) { headers[MAP][name] = [obj[name]]; } } return headers; } const INTERNALS$1 = Symbol("Response internals"); const STATUS_CODES = http2.default.STATUS_CODES; class Response { constructor() { let body2 = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : null; let opts = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}; Body.call(this, body2, opts); const status = opts.status || 200; const headers = new Headers(opts.headers); if (body2 != null && !headers.has("Content-Type")) { const contentType = extractContentType(body2); if (contentType) { headers.append("Content-Type", contentType); } } this[INTERNALS$1] = { url: opts.url, status, statusText: opts.statusText || STATUS_CODES[status], headers, counter: opts.counter }; } get url() { return this[INTERNALS$1].url || ""; } get status() { return this[INTERNALS$1].status; } get ok() { return this[INTERNALS$1].status >= 200 && this[INTERNALS$1].status < 300; } get redirected() { return this[INTERNALS$1].counter > 0; } get statusText() { return this[INTERNALS$1].statusText; } get headers() { return this[INTERNALS$1].headers; } clone() { return new Response(clone(this), { url: this.url, status: this.status, statusText: this.statusText, headers: this.headers, ok: this.ok, redirected: this.redirected }); } } Body.mixIn(Response.prototype); Object.defineProperties(Response.prototype, { url: {enumerable: true}, status: {enumerable: true}, ok: {enumerable: true}, redirected: {enumerable: true}, statusText: {enumerable: true}, headers: {enumerable: true}, clone: {enumerable: true} }); Object.defineProperty(Response.prototype, Symbol.toStringTag, { value: "Response", writable: false, enumerable: false, configurable: true }); const INTERNALS$2 = Symbol("Request internals"); const parse_url = url.default.parse; const format_url = url.default.format; const streamDestructionSupported = "destroy" in stream.default.Readable.prototype; function isRequest(input) { return typeof input === "object" && typeof input[INTERNALS$2] === "object"; } function isAbortSignal(signal) { const proto = signal && typeof signal === "object" && Object.getPrototypeOf(signal); return !!(proto && proto.constructor.name === "AbortSignal"); } class Request { constructor(input) { let init2 = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}; let parsedURL; if (!isRequest(input)) { if (input && input.href) { parsedURL = parse_url(input.href); } else { parsedURL = parse_url(`${input}`); } input = {}; } else { parsedURL = parse_url(input.url); } let method = init2.method || input.method || "GET"; method = method.toUpperCase(); if ((init2.body != null || isRequest(input) && input.body !== null) && (method === "GET" || method === "HEAD")) { throw new TypeError("Request with GET/HEAD method cannot have body"); } let inputBody = init2.body != null ? init2.body : isRequest(input) && input.body !== null ? clone(input) : null; Body.call(this, inputBody, { timeout: init2.timeout || input.timeout || 0, size: init2.size || input.size || 0 }); const headers = new Headers(init2.headers || input.headers || {}); if (inputBody != null && !headers.has("Content-Type")) { const contentType = extractContentType(inputBody); if (contentType) { headers.append("Content-Type", contentType); } } let signal = isRequest(input) ? input.signal : null; if ("signal" in init2) signal = init2.signal; if (signal != null && !isAbortSignal(signal)) { throw new TypeError("Expected signal to be an instanceof AbortSignal"); } this[INTERNALS$2] = { method, redirect: init2.redirect || input.redirect || "follow", headers, parsedURL, signal }; this.follow = init2.follow !== void 0 ? init2.follow : input.follow !== void 0 ? input.follow : 20; this.compress = init2.compress !== void 0 ? init2.compress : input.compress !== void 0 ? input.compress : true; this.counter = init2.counter || input.counter || 0; this.agent = init2.agent || input.agent; } get method() { return this[INTERNALS$2].method; } get url() { return format_url(this[INTERNALS$2].parsedURL); } get headers() { return this[INTERNALS$2].headers; } get redirect() { return this[INTERNALS$2].redirect; } get signal() { return this[INTERNALS$2].signal; } clone() { return new Request(this); } } Body.mixIn(Request.prototype); Object.defineProperty(Request.prototype, Symbol.toStringTag, { value: "Request", writable: false, enumerable: false, configurable: true }); Object.defineProperties(Request.prototype, { method: {enumerable: true}, url: {enumerable: true}, headers: {enumerable: true}, redirect: {enumerable: true}, clone: {enumerable: true}, signal: {enumerable: true} }); function getNodeRequestOptions(request) { const parsedURL = request[INTERNALS$2].parsedURL; const headers = new Headers(request[INTERNALS$2].headers); if (!headers.has("Accept")) { headers.set("Accept", "*/*"); } if (!parsedURL.protocol || !parsedURL.hostname) { throw new TypeError("Only absolute URLs are supported"); } if (!/^https?:$/.test(parsedURL.protocol)) { throw new TypeError("Only HTTP(S) protocols are supported"); } if (request.signal && request.body instanceof stream.default.Readable && !streamDestructionSupported) { throw new Error("Cancellation of streamed requests with AbortSignal is not supported in node < 8"); } let contentLengthValue = null; if (request.body == null && /^(POST|PUT)$/i.test(request.method)) { contentLengthValue = "0"; } if (request.body != null) { const totalBytes = getTotalBytes(request); if (typeof totalBytes === "number") { contentLengthValue = String(totalBytes); } } if (contentLengthValue) { headers.set("Content-Length", contentLengthValue); } if (!headers.has("User-Agent")) { headers.set("User-Agent", "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)"); } if (request.compress && !headers.has("Accept-Encoding")) { headers.set("Accept-Encoding", "gzip,deflate"); } let agent = request.agent; if (typeof agent === "function") { agent = agent(parsedURL); } if (!headers.has("Connection") && !agent) { headers.set("Connection", "close"); } return Object.assign({}, parsedURL, { method: request.method, headers: exportNodeCompatibleHeaders(headers), agent }); } function AbortError(message) { Error.call(this, message); this.type = "aborted"; this.message = message; Error.captureStackTrace(this, this.constructor); } AbortError.prototype = Object.create(Error.prototype); AbortError.prototype.constructor = AbortError; AbortError.prototype.name = "AbortError"; const PassThrough$1 = stream.default.PassThrough; const resolve_url = url.default.resolve; function fetch2(url2, opts) { if (!fetch2.Promise) { throw new Error("native promise missing, set fetch.Promise to your favorite alternative"); } Body.Promise = fetch2.Promise; return new fetch2.Promise(function(resolve, reject) { const request = new Request(url2, opts); const options = getNodeRequestOptions(request); const send = (options.protocol === "https:" ? https2.default : http2.default).request; const signal = request.signal; let response = null; const abort = function abort2() { let error = new AbortError("The user aborted a request."); reject(error); if (request.body && request.body instanceof stream.default.Readable) { request.body.destroy(error); } if (!response || !response.body) return; response.body.emit("error", error); }; if (signal && signal.aborted) { abort(); return; } const abortAndFinalize = function abortAndFinalize2() { abort(); finalize(); }; const req = send(options); let reqTimeout; if (signal) { signal.addEventListener("abort", abortAndFinalize); } function finalize() { req.abort(); if (signal) signal.removeEventListener("abort", abortAndFinalize); clearTimeout(reqTimeout); } if (request.timeout) { req.once("socket", function(socket) { reqTimeout = setTimeout(function() { reject(new FetchError(`network timeout at: ${request.url}`, "request-timeout")); finalize(); }, request.timeout); }); } req.on("error", function(err) { reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, "system", err)); finalize(); }); req.on("response", function(res) { clearTimeout(reqTimeout); const headers = createHeadersLenient(res.headers); if (fetch2.isRedirect(res.statusCode)) { const location = headers.get("Location"); const locationURL = location === null ? null : resolve_url(request.url, location); switch (request.redirect) { case "error": reject(new FetchError(`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`, "no-redirect")); finalize(); return; case "manual": if (locationURL !== null) { try { headers.set("Location", locationURL); } catch (err) { reject(err); } } break; case "follow": if (locationURL === null) { break; } if (request.counter >= request.follow) { reject(new FetchError(`maximum redirect reached at: ${request.url}`, "max-redirect")); finalize(); return; } const requestOpts = { headers: new Headers(request.headers), follow: request.follow, counter: request.counter + 1, agent: request.agent, compress: request.compress, method: request.method, body: request.body, signal: request.signal, timeout: request.timeout, size: request.size }; if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) { reject(new FetchError("Cannot follow redirect with body being a readable stream", "unsupported-redirect")); finalize(); return; } if (res.statusCode === 303 || (res.statusCode === 301 || res.statusCode === 302) && request.method === "POST") { requestOpts.method = "GET"; requestOpts.body = void 0; requestOpts.headers.delete("content-length"); } resolve(fetch2(new Request(locationURL, requestOpts))); finalize(); return; } } res.once("end", function() { if (signal) signal.removeEventListener("abort", abortAndFinalize); }); let body2 = res.pipe(new PassThrough$1()); const response_options = { url: request.url, status: res.statusCode, statusText: res.statusMessage, headers, size: request.size, timeout: request.timeout, counter: request.counter }; const codings = headers.get("Content-Encoding"); if (!request.compress || request.method === "HEAD" || codings === null || res.statusCode === 204 || res.statusCode === 304) { response = new Response(body2, response_options); resolve(response); return; } const zlibOptions = { flush: zlib2.default.Z_SYNC_FLUSH, finishFlush: zlib2.default.Z_SYNC_FLUSH }; if (codings == "gzip" || codings == "x-gzip") { body2 = body2.pipe(zlib2.default.createGunzip(zlibOptions)); response = new Response(body2, response_options); resolve(response); return; } if (codings == "deflate" || codings == "x-deflate") { const raw = res.pipe(new PassThrough$1()); raw.once("data", function(chunk) { if ((chunk[0] & 15) === 8) { body2 = body2.pipe(zlib2.default.createInflate()); } else { body2 = body2.pipe(zlib2.default.createInflateRaw()); } response = new Response(body2, response_options); resolve(response); }); return; } if (codings == "br" && typeof zlib2.default.createBrotliDecompress === "function") { body2 = body2.pipe(zlib2.default.createBrotliDecompress()); response = new Response(body2, response_options); resolve(response); return; } response = new Response(body2, response_options); resolve(response); }); writeToStream(req, request); }); } fetch2.isRedirect = function(code) { return code === 301 || code === 302 || code === 303 || code === 307 || code === 308; }; fetch2.Promise = global.Promise; var lib_default = fetch2; }); // node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js var require_tf_core_node = __commonJS((exports2) => { /** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ "use strict"; Object.defineProperty(exports2, "__esModule", {value: true}); /*! ***************************************************************************** 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. ***************************************************************************** */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || {__proto__: []} instanceof Array && function(d2, b2) { d2.__proto__ = b2; } || function(d2, b2) { for (var p in b2) if (b2.hasOwnProperty(p)) d2[p] = b2[p]; }; return extendStatics(d, b); }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } function __awaiter(thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function(resolve, reject) { function fulfilled(value) { try { step2(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step2(generator["throw"](value)); } catch (e) { reject(e); } } function step2(result) { result.done ? resolve(result.value) : new P(function(resolve2) { resolve2(result.value); }).then(fulfilled, rejected); } step2((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body2) { 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 step2([n, v]); }; } function step2(op2) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op2[0] & 2 ? y["return"] : op2[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op2[1])).done) return t; if (y = 0, t) op2 = [op2[0] & 2, t.value]; switch (op2[0]) { case 0: case 1: t = op2; break; case 4: _.label++; return {value: op2[1], done: false}; case 5: _.label++; y = op2[1]; op2 = [0]; continue; case 7: op2 = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op2[0] === 6 || op2[0] === 2)) { _ = 0; continue; } if (op2[0] === 3 && (!t || op2[1] > t[0] && op2[1] < t[3])) { _.label = op2[1]; break; } if (op2[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op2; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op2); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op2 = body2.call(thisArg, _); } catch (e) { op2 = [6, e]; y = 0; } finally { f = t = 0; } if (op2[0] & 5) throw op2[1]; return {value: op2[0] ? op2[1] : void 0, done: true}; } } /** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ var EPSILON_FLOAT32 = 1e-7; var EPSILON_FLOAT16 = 1e-4; var DataStorage2 = function() { function DataStorage3(backend2, dataMover) { this.backend = backend2; this.dataMover = dataMover; this.data = new WeakMap(); this.dataIdsCount = 0; } DataStorage3.prototype.get = function(dataId) { if (!this.data.has(dataId)) { this.dataMover.moveData(this.backend, dataId); } return this.data.get(dataId); }; DataStorage3.prototype.set = function(dataId, value) { this.dataIdsCount++; this.data.set(dataId, value); }; DataStorage3.prototype.has = function(dataId) { return this.data.has(dataId); }; DataStorage3.prototype.delete = function(dataId) { this.dataIdsCount--; return this.data.delete(dataId); }; DataStorage3.prototype.numDataIds = function() { return this.dataIdsCount; }; return DataStorage3; }(); var KernelBackend2 = function() { function KernelBackend3() { } KernelBackend3.prototype.time = function(f) { return notYetImplemented("time"); }; KernelBackend3.prototype.read = function(dataId) { return notYetImplemented("read"); }; KernelBackend3.prototype.readSync = function(dataId) { return notYetImplemented("readSync"); }; KernelBackend3.prototype.numDataIds = function() { return notYetImplemented("numDataIds"); }; KernelBackend3.prototype.disposeData = function(dataId) { return notYetImplemented("disposeData"); }; KernelBackend3.prototype.write = function(values, shape, dtype) { return notYetImplemented("write"); }; KernelBackend3.prototype.move = function(dataId, values, shape, dtype) { return notYetImplemented("move"); }; KernelBackend3.prototype.memory = function() { return notYetImplemented("memory"); }; KernelBackend3.prototype.floatPrecision = function() { return notYetImplemented("floatPrecision"); }; KernelBackend3.prototype.epsilon = function() { return this.floatPrecision() === 32 ? EPSILON_FLOAT32 : EPSILON_FLOAT16; }; KernelBackend3.prototype.batchMatMul = function(a, b, transposeA, transposeB) { return notYetImplemented("batchMatMul"); }; KernelBackend3.prototype.fusedBatchMatMul = function(_a) { var a = _a.a, b = _a.b, transposeA = _a.transposeA, transposeB = _a.transposeB, bias = _a.bias, activation = _a.activation, preluActivationWeights = _a.preluActivationWeights; return notYetImplemented("fusedBatchMatMul"); }; KernelBackend3.prototype.slice = function(x, begin, size) { return notYetImplemented("slice"); }; KernelBackend3.prototype.stridedSlice = function(x, begin, end, strides) { return notYetImplemented("stridedSlice"); }; KernelBackend3.prototype.unstack = function(x, axis) { return notYetImplemented("unstack"); }; KernelBackend3.prototype.reverse = function(a, axis) { return notYetImplemented("reverse"); }; KernelBackend3.prototype.concat = function(tensors, axis) { return notYetImplemented("concat"); }; KernelBackend3.prototype.neg = function(a) { return notYetImplemented("neg"); }; KernelBackend3.prototype.add = function(a, b) { return notYetImplemented("add"); }; KernelBackend3.prototype.addN = function(tensors) { return notYetImplemented("addN"); }; KernelBackend3.prototype.subtract = function(a, b) { return notYetImplemented("subtract"); }; KernelBackend3.prototype.multiply = function(a, b) { return notYetImplemented("multiply"); }; KernelBackend3.prototype.realDivide = function(a, b) { return notYetImplemented("realDivide"); }; KernelBackend3.prototype.floorDiv = function(a, b) { return notYetImplemented("floorDiv"); }; KernelBackend3.prototype.sum = function(x, axes) { return notYetImplemented("sum"); }; KernelBackend3.prototype.prod = function(x, axes) { return notYetImplemented("prod"); }; KernelBackend3.prototype.unsortedSegmentSum = function(x, segmentIds, numSegments) { return notYetImplemented("unsortedSegmentSum"); }; KernelBackend3.prototype.argMin = function(x, axis) { return notYetImplemented("argMin"); }; KernelBackend3.prototype.argMax = function(x, axis) { return notYetImplemented("argMax"); }; KernelBackend3.prototype.equal = function(a, b) { return notYetImplemented("equal"); }; KernelBackend3.prototype.notEqual = function(a, b) { return notYetImplemented("notEqual"); }; KernelBackend3.prototype.less = function(a, b) { return notYetImplemented("less"); }; KernelBackend3.prototype.lessEqual = function(a, b) { return notYetImplemented("lessEqual"); }; KernelBackend3.prototype.greater = function(a, b) { return notYetImplemented("greater"); }; KernelBackend3.prototype.greaterEqual = function(a, b) { return notYetImplemented("greaterEqual"); }; KernelBackend3.prototype.logicalNot = function(a) { return notYetImplemented("logicalNot"); }; KernelBackend3.prototype.logicalAnd = function(a, b) { return notYetImplemented("logicalAnd"); }; KernelBackend3.prototype.logicalOr = function(a, b) { return notYetImplemented("logicalOr"); }; KernelBackend3.prototype.where = function(condition) { return notYetImplemented("where"); }; KernelBackend3.prototype.select = function(condition, a, b) { return notYetImplemented("select"); }; KernelBackend3.prototype.topk = function(x, k, sorted) { return notYetImplemented("topk"); }; KernelBackend3.prototype.min = function(x, axes) { return notYetImplemented("min"); }; KernelBackend3.prototype.minimum = function(a, b) { return notYetImplemented("minimum"); }; KernelBackend3.prototype.mod = function(a, b) { return notYetImplemented("mod"); }; KernelBackend3.prototype.max = function(x, axes) { return notYetImplemented("max"); }; KernelBackend3.prototype.maximum = function(a, b) { return notYetImplemented("maximum"); }; KernelBackend3.prototype.all = function(x, axes) { return notYetImplemented("all"); }; KernelBackend3.prototype.any = function(x, axes) { return notYetImplemented("any"); }; KernelBackend3.prototype.squaredDifference = function(a, b) { return notYetImplemented("squaredDifference"); }; KernelBackend3.prototype.ceil = function(x) { return notYetImplemented("ceil"); }; KernelBackend3.prototype.floor = function(x) { return notYetImplemented("floor"); }; KernelBackend3.prototype.round = function(x) { return notYetImplemented("round"); }; KernelBackend3.prototype.sign = function(x) { return notYetImplemented("sign"); }; KernelBackend3.prototype.isNaN = function(x) { return notYetImplemented("isNaN"); }; KernelBackend3.prototype.isInf = function(x) { return notYetImplemented("isInf"); }; KernelBackend3.prototype.isFinite = function(x) { return notYetImplemented("isFinite"); }; KernelBackend3.prototype.pow = function(a, b) { return notYetImplemented("pow"); }; KernelBackend3.prototype.exp = function(x) { return notYetImplemented("exp"); }; KernelBackend3.prototype.expm1 = function(x) { return notYetImplemented("expm1"); }; KernelBackend3.prototype.softmax = function(x, dim) { return notYetImplemented("softmax"); }; KernelBackend3.prototype.log = function(x) { return notYetImplemented("log"); }; KernelBackend3.prototype.log1p = function(x) { return notYetImplemented("log1p"); }; KernelBackend3.prototype.sqrt = function(x) { return notYetImplemented("sqrt"); }; KernelBackend3.prototype.rsqrt = function(x) { return notYetImplemented("rsqrt"); }; KernelBackend3.prototype.square = function(x) { return notYetImplemented("square"); }; KernelBackend3.prototype.reciprocal = function(x) { return notYetImplemented("reciprocal"); }; KernelBackend3.prototype.relu = function(x) { return notYetImplemented("relu"); }; KernelBackend3.prototype.relu6 = function(x) { return notYetImplemented("relu6"); }; KernelBackend3.prototype.prelu = function(x, a) { return notYetImplemented("prelu"); }; KernelBackend3.prototype.elu = function(x) { return notYetImplemented("elu"); }; KernelBackend3.prototype.eluDer = function(dy, y) { return notYetImplemented("eluDer"); }; KernelBackend3.prototype.selu = function(x) { return notYetImplemented("selu"); }; KernelBackend3.prototype.int = function(x) { return notYetImplemented("int"); }; KernelBackend3.prototype.clip = function(x, min3, max3) { return notYetImplemented("clip"); }; KernelBackend3.prototype.abs = function(x) { return notYetImplemented("abs"); }; KernelBackend3.prototype.complexAbs = function(x) { return notYetImplemented("complexAbs"); }; KernelBackend3.prototype.sigmoid = function(x) { return notYetImplemented("sigmoid"); }; KernelBackend3.prototype.softplus = function(x) { return notYetImplemented("softplus"); }; KernelBackend3.prototype.sin = function(x) { return notYetImplemented("sin"); }; KernelBackend3.prototype.cos = function(x) { return notYetImplemented("cos"); }; KernelBackend3.prototype.tan = function(x) { return notYetImplemented("tan"); }; KernelBackend3.prototype.asin = function(x) { return notYetImplemented("asin"); }; KernelBackend3.prototype.acos = function(x) { return notYetImplemented("acos"); }; KernelBackend3.prototype.atan = function(x) { return notYetImplemented("atan"); }; KernelBackend3.prototype.atan2 = function(a, b) { return notYetImplemented("atan2"); }; KernelBackend3.prototype.sinh = function(x) { return notYetImplemented("sinh"); }; KernelBackend3.prototype.cosh = function(x) { return notYetImplemented("cosh"); }; KernelBackend3.prototype.tanh = function(x) { return notYetImplemented("tanh"); }; KernelBackend3.prototype.asinh = function(x) { return notYetImplemented("asinh"); }; KernelBackend3.prototype.acosh = function(x) { return notYetImplemented("acosh"); }; KernelBackend3.prototype.atanh = function(x) { return notYetImplemented("atanh"); }; KernelBackend3.prototype.erf = function(x) { return notYetImplemented("erf"); }; KernelBackend3.prototype.step = function(x, alpha) { return notYetImplemented("step"); }; KernelBackend3.prototype.fusedConv2d = function(_a) { var input = _a.input, filter = _a.filter, convInfo = _a.convInfo, bias = _a.bias, activation = _a.activation, preluActivationWeights = _a.preluActivationWeights; return notYetImplemented("fusedConv2d"); }; KernelBackend3.prototype.conv2d = function(x, filter, convInfo) { return notYetImplemented("conv2d"); }; KernelBackend3.prototype.conv2dDerInput = function(dy, filter, convInfo) { return notYetImplemented("conv2dDerInput"); }; KernelBackend3.prototype.conv2dDerFilter = function(x, dY, convInfo) { return notYetImplemented("conv2dDerFilter"); }; KernelBackend3.prototype.fusedDepthwiseConv2D = function(_a) { var input = _a.input, filter = _a.filter, convInfo = _a.convInfo, bias = _a.bias, activation = _a.activation, preluActivationWeights = _a.preluActivationWeights; return notYetImplemented("fusedDepthwiseConv2D"); }; KernelBackend3.prototype.depthwiseConv2D = function(input, filter, convInfo) { return notYetImplemented("depthwiseConv2D"); }; KernelBackend3.prototype.depthwiseConv2DDerInput = function(dy, filter, convInfo) { return notYetImplemented("depthwiseConv2DDerInput"); }; KernelBackend3.prototype.depthwiseConv2DDerFilter = function(x, dY, convInfo) { return notYetImplemented("depthwiseConv2DDerFilter"); }; KernelBackend3.prototype.conv3d = function(x, filter, convInfo) { return notYetImplemented("conv3d"); }; KernelBackend3.prototype.conv3dDerInput = function(dy, filter, convInfo) { return notYetImplemented("conv3dDerInput"); }; KernelBackend3.prototype.conv3dDerFilter = function(x, dY, convInfo) { return notYetImplemented("conv3dDerFilter"); }; KernelBackend3.prototype.maxPool = function(x, convInfo) { return notYetImplemented("maxPool"); }; KernelBackend3.prototype.maxPoolBackprop = function(dy, x, y, convInfo) { return notYetImplemented("maxPoolBackprop"); }; KernelBackend3.prototype.avgPool = function(x, convInfo) { return notYetImplemented("avgPool"); }; KernelBackend3.prototype.avgPoolBackprop = function(dy, x, convInfo) { return notYetImplemented("avgPoolBackprop"); }; KernelBackend3.prototype.avgPool3d = function(x, convInfo) { return notYetImplemented("avgPool3d"); }; KernelBackend3.prototype.avgPool3dBackprop = function(dy, x, convInfo) { return notYetImplemented("avgPool3dBackprop"); }; KernelBackend3.prototype.maxPool3d = function(x, convInfo) { return notYetImplemented("maxPool3d"); }; KernelBackend3.prototype.maxPool3dBackprop = function(dy, x, y, convInfo) { return notYetImplemented("maxPool3dBackprop"); }; KernelBackend3.prototype.reshape = function(x, shape) { return notYetImplemented("reshape"); }; KernelBackend3.prototype.cast = function(x, dtype) { return notYetImplemented("cast"); }; KernelBackend3.prototype.tile = function(x, reps) { return notYetImplemented("tile"); }; KernelBackend3.prototype.pad = function(x, paddings, constantValue) { return notYetImplemented("pad"); }; KernelBackend3.prototype.transpose = function(x, perm) { return notYetImplemented("transpose"); }; KernelBackend3.prototype.gather = function(x, indices, axis) { return notYetImplemented("gather"); }; KernelBackend3.prototype.gatherND = function(x, indices) { return notYetImplemented("gatherND"); }; KernelBackend3.prototype.scatterND = function(indices, updates, shape) { return notYetImplemented("scatterND"); }; KernelBackend3.prototype.batchToSpaceND = function(x, blockShape, crops) { return notYetImplemented("batchToSpaceND"); }; KernelBackend3.prototype.spaceToBatchND = function(x, blockShape, paddings) { return notYetImplemented("spaceToBatchND"); }; KernelBackend3.prototype.resizeBilinear = function(x, newHeight, newWidth, alignCorners) { return notYetImplemented("resizeBilinear"); }; KernelBackend3.prototype.resizeBilinearBackprop = function(dy, x, alignCorners) { return notYetImplemented("resizeBilinearBackprop"); }; KernelBackend3.prototype.resizeNearestNeighbor = function(x, newHEight, newWidth, alignCorners) { return notYetImplemented("resizeNearestNeighbor"); }; KernelBackend3.prototype.resizeNearestNeighborBackprop = function(dy, x, alignCorners) { return notYetImplemented("resizeNearestNeighborBackprop"); }; KernelBackend3.prototype.batchNorm = function(x, mean2, variance, offset, scale, varianceEpsilon) { return notYetImplemented("batchNorm"); }; KernelBackend3.prototype.localResponseNormalization4D = function(x, radius, bias, alpha, beta) { return notYetImplemented("localResponseNormalization4D"); }; KernelBackend3.prototype.LRNGrad = function(dy, inputImage, outputImage, radius, bias, alpha, beta) { return notYetImplemented("LRNGrad"); }; KernelBackend3.prototype.multinomial = function(logits, normalized, numSamples, seed) { return notYetImplemented("multinomial"); }; KernelBackend3.prototype.oneHot = function(indices, depth, onValue, offValue) { return notYetImplemented("oneHot"); }; KernelBackend3.prototype.cumsum = function(x, axis, exclusive, reverse3) { return notYetImplemented("cumsum"); }; KernelBackend3.prototype.nonMaxSuppression = function(boxes, scores, maxOutputSize, iouThreshold, scoreThreshold) { return notYetImplemented("nonMaxSuppression"); }; KernelBackend3.prototype.fft = function(x) { return notYetImplemented("fft"); }; KernelBackend3.prototype.ifft = function(x) { return notYetImplemented("ifft"); }; KernelBackend3.prototype.complex = function(real2, imag2) { return notYetImplemented("complex"); }; KernelBackend3.prototype.real = function(input) { return notYetImplemented("real"); }; KernelBackend3.prototype.imag = function(input) { return notYetImplemented("imag"); }; KernelBackend3.prototype.cropAndResize = function(image3, boxes, boxIndex, cropSize, method, extrapolationValue) { return notYetImplemented("cropAndResize"); }; KernelBackend3.prototype.depthToSpace = function(x, blockSize, dataFormat) { return notYetImplemented("depthToSpace"); }; KernelBackend3.prototype.split = function(value, sizeSplits, axis) { return notYetImplemented("split"); }; KernelBackend3.prototype.sparseToDense = function(sparseIndices, sparseValues, outputShape, defaultValue) { return notYetImplemented("sparseToDense"); }; KernelBackend3.prototype.diag = function(x) { return notYetImplemented("diag"); }; KernelBackend3.prototype.fill = function(shape, value, dtype) { return notYetImplemented("fill"); }; KernelBackend3.prototype.onesLike = function(x) { return notYetImplemented("onesLike"); }; KernelBackend3.prototype.zerosLike = function(x) { return notYetImplemented("zerosLike"); }; KernelBackend3.prototype.linspace = function(start, stop, num) { return notYetImplemented("linspace"); }; KernelBackend3.prototype.dispose = function() { return notYetImplemented("dispose"); }; return KernelBackend3; }(); function notYetImplemented(kernelName) { throw new Error("'" + kernelName + "' not yet implemented or not found in the registry. This kernel may not be supported by the tfjs backend you have chosen"); } /** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ 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(min3, x, max3) { return Math.max(min3, Math.min(x, max3)); } function nearestLargerEven(val) { return val % 2 === 0 ? val : val + 1; } function sum2(arr) { var sum3 = 0; for (var i = 0; i < arr.length; i++) { sum3 += arr[i]; } return sum3; } function randUniform(a, b) { var r = Math.random(); return b * r + (1 - r) * 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(typeof msg === "string" ? msg : msg()); } } function assertShapesMatch(shapeA, shapeB, errorMessagePrefix) { if (errorMessagePrefix === void 0) { errorMessagePrefix = ""; } assert(arraysEqual(shapeA, shapeB), function() { return errorMessagePrefix + (" Shapes " + shapeA + " and " + shapeB + " must match"); }); } function assertNonNull(a) { assert(a != null, function() { return "The input to the tensor constructor must be a non-null value."; }); } function flatten(arr, result, skipTypedArray) { if (result === void 0) { result = []; } if (skipTypedArray === void 0) { skipTypedArray = false; } if (result == null) { result = []; } if (Array.isArray(arr) || isTypedArray(arr) && !skipTypedArray) { for (var i = 0; i < arr.length; ++i) { flatten(arr[i], result, skipTypedArray); } } else { result.push(arr); } return result; } 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 === n2) { return true; } if (n1 == null || n2 == null) { return false; } 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) { var width = Math.ceil(Math.sqrt(size)); return [width, Math.ceil(size / width)]; } 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); }; tryFn(); }); } 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 (shapeProd === 0) { throw Error("Cannot infer the missing size in [" + shape + "] when there are 0 elements"); } 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 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; }), function() { return "All values in axis param must be in range [-" + rank + ", " + rank + ") but " + ("got axis " + axis); }); assert(axis.every(function(ax) { return isInt(ax); }), function() { return "All values in axis param must be integers but " + ("got axis " + axis); }); return axis.map(function(a) { return a < 0 ? rank + a : a; }); } function squeezeShape(shape, axis) { var newShape = []; var keptDims = []; var isEmptyArray = axis != null && Array.isArray(axis) && axis.length === 0; var axes = axis == null || isEmptyArray ? null : parseAxisParam(axis, shape).sort(); var j = 0; for (var i = 0; i < shape.length; ++i) { if (axes != null) { if (axes[j] === i && shape[i] !== 1) { throw new Error("Can't squeeze axis " + i + " since its dim '" + shape[i] + "' is not 1"); } if ((axes[j] == null || axes[j] > i) && shape[i] === 1) { newShape.push(shape[i]); keptDims.push(i); } if (axes[j] <= i) { j++; } } if (shape[i] !== 1) { newShape.push(shape[i]); keptDims.push(i); } } return {newShape, 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 getArrayFromDType(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 if (dtype === "string") { values = new Array(size); } else { throw new Error("Unknown data type " + dtype); } return values; } function checkConversionForErrors(vals, dtype) { for (var i = 0; i < vals.length; i++) { var num = vals[i]; if (isNaN(num) || !isFinite(num)) { throw Error("A tensor of type " + dtype + " being uploaded contains " + num + "."); } } } function isValidDtype(dtype) { return dtype === "bool" || dtype === "complex64" || dtype === "float32" || dtype === "int32" || dtype === "string"; } function hasEncodingLoss(oldType, newType) { if (newType === "complex64") { return false; } if (newType === "float32" && oldType !== "complex64") { return false; } if (newType === "int32" && oldType !== "float32" && oldType !== "complex64") { return false; } if (newType === "bool" && oldType === "bool") { return false; } return true; } 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 === "complex64") { return 8; } else if (dtype === "bool") { return 1; } else { throw new Error("Unknown dtype " + dtype); } } function bytesFromStringArray(arr) { if (arr == null) { return 0; } var bytes = 0; arr.forEach(function(x) { return bytes += x.length; }); return bytes; } function isString(value) { return typeof value === "string" || value instanceof String; } function isBoolean(value) { return typeof value === "boolean"; } function isNumber(value) { return typeof value === "number"; } function inferDtype(values) { if (Array.isArray(values)) { return inferDtype(values[0]); } if (values instanceof Float32Array) { return "float32"; } else if (values instanceof Int32Array || values instanceof Uint8Array) { return "int32"; } else if (isNumber(values)) { return "float32"; } else if (isString(values)) { return "string"; } else if (isBoolean(values)) { return "bool"; } return "float32"; } function isFunction(f) { return !!(f && f.constructor && f.call && f.apply); } function nearestDivisor(size, start) { for (var i = start; i < size; ++i) { if (size % i === 0) { return i; } } return size; } 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; } function createNestedArray(offset, shape, a) { var ret = new Array(); if (shape.length === 1) { var d = shape[0]; for (var i = 0; i < d; i++) { ret[i] = a[offset + i]; } } else { var d = shape[0]; var rest = shape.slice(1); var len = rest.reduce(function(acc, c) { return acc * c; }); for (var i = 0; i < d; i++) { ret[i] = createNestedArray(offset + i * len, rest, a); } } return ret; } function toNestedArray(shape, a) { if (shape.length === 0) { return a[0]; } var size = shape.reduce(function(acc, c) { return acc * c; }); if (size === 0) { return []; } if (size !== a.length) { throw new Error("[" + shape + "] does not match the input size " + a.length + "."); } return createNestedArray(0, shape, a); } function makeOnesTypedArray(size, dtype) { var array = makeZerosTypedArray(size, dtype); for (var i = 0; i < array.length; i++) { array[i] = 1; } return array; } function makeZerosTypedArray(size, dtype) { if (dtype == null || dtype === "float32" || dtype === "complex64") { 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 makeZerosNestedTypedArray(shape, dtype) { var size = shape.reduce(function(prev, curr) { return prev * curr; }, 1); if (dtype == null || dtype === "float32") { return toNestedArray(shape, new Float32Array(size)); } else if (dtype === "int32") { return toNestedArray(shape, new Int32Array(size)); } else if (dtype === "bool") { return toNestedArray(shape, new Uint8Array(size)); } else { throw new Error("Unknown data type " + dtype); } } function assertNonNegativeIntegerDimensions(shape) { shape.forEach(function(dimSize) { assert(Number.isInteger(dimSize) && dimSize >= 0, function() { return "Tensor must have a shape comprised of positive integers but got " + ("shape [" + shape + "]."); }); }); } function locToIndex(locs, rank, strides) { if (rank === 0) { return 0; } else if (rank === 1) { return locs[0]; } var index = locs[locs.length - 1]; for (var i = 0; i < locs.length - 1; ++i) { index += strides[i] * locs[i]; } return index; } function indexToLoc(index, rank, strides) { if (rank === 0) { return []; } else if (rank === 1) { return [index]; } var locs = new Array(rank); for (var i = 0; i < locs.length - 1; ++i) { locs[i] = Math.floor(index / strides[i]); index -= locs[i] * strides[i]; } locs[locs.length - 1] = index; return locs; } function isPromise(object) { return object && object.then && typeof object.then === "function"; } /** * @license * Copyright 2017 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ var TENSORFLOWJS_FLAGS_PREFIX = "tfjsflags"; var Environment = function() { function Environment2(global2) { this.global = global2; this.flags = {}; this.flagRegistry = {}; this.urlFlags = {}; this.populateURLFlags(); } Environment2.prototype.setPlatform = function(platformName, platform) { if (this.platform != null) { console.warn("Platform " + this.platformName + " has already been set. " + ("Overwriting the platform with " + platform + ".")); } this.platformName = platformName; this.platform = platform; }; Environment2.prototype.registerFlag = function(flagName, evaluationFn, setHook) { this.flagRegistry[flagName] = {evaluationFn, setHook}; if (this.urlFlags[flagName] != null) { var flagValue = this.urlFlags[flagName]; console.warn("Setting feature override from URL " + flagName + ": " + flagValue + "."); this.set(flagName, flagValue); } }; Environment2.prototype.getAsync = function(flagName) { return __awaiter(this, void 0, void 0, function() { var _a, _b; return __generator(this, function(_c) { switch (_c.label) { case 0: if (flagName in this.flags) { return [2, this.flags[flagName]]; } _a = this.flags; _b = flagName; return [4, this.evaluateFlag(flagName)]; case 1: _a[_b] = _c.sent(); return [2, this.flags[flagName]]; } }); }); }; Environment2.prototype.get = function(flagName) { if (flagName in this.flags) { return this.flags[flagName]; } var flagValue = this.evaluateFlag(flagName); if (isPromise(flagValue)) { throw new Error("Flag " + flagName + " cannot be synchronously evaluated. Please use getAsync() instead."); } this.flags[flagName] = flagValue; return this.flags[flagName]; }; Environment2.prototype.getNumber = function(flagName) { return this.get(flagName); }; Environment2.prototype.getBool = function(flagName) { return this.get(flagName); }; Environment2.prototype.getFlags = function() { return this.flags; }; Object.defineProperty(Environment2.prototype, "features", { get: function() { return this.flags; }, enumerable: true, configurable: true }); Environment2.prototype.set = function(flagName, value) { if (this.flagRegistry[flagName] == null) { throw new Error("Cannot set flag " + flagName + " as it has not been registered."); } this.flags[flagName] = value; if (this.flagRegistry[flagName].setHook != null) { this.flagRegistry[flagName].setHook(value); } }; Environment2.prototype.evaluateFlag = function(flagName) { if (this.flagRegistry[flagName] == null) { throw new Error("Cannot evaluate flag '" + flagName + "': no evaluation function found."); } return this.flagRegistry[flagName].evaluationFn(); }; Environment2.prototype.setFlags = function(flags) { this.flags = Object.assign({}, flags); }; Environment2.prototype.reset = function() { this.flags = {}; this.urlFlags = {}; this.populateURLFlags(); }; Environment2.prototype.populateURLFlags = function() { var _this = this; if (typeof this.global === "undefined" || typeof this.global.location === "undefined" || typeof this.global.location.search === "undefined") { return; } var urlParams = getQueryParams(this.global.location.search); if (TENSORFLOWJS_FLAGS_PREFIX in urlParams) { var keyValues = urlParams[TENSORFLOWJS_FLAGS_PREFIX].split(","); keyValues.forEach(function(keyValue) { var _a = keyValue.split(":"), key = _a[0], value = _a[1]; _this.urlFlags[key] = parseValue(key, value); }); } }; return Environment2; }(); function getQueryParams(queryString) { var params = {}; queryString.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g, function(s) { var t = []; for (var _i2 = 1; _i2 < arguments.length; _i2++) { t[_i2 - 1] = arguments[_i2]; } decodeParam(params, t[0], t[1]); return t.join("="); }); return params; } function decodeParam(params, name, value) { params[decodeURIComponent(name)] = decodeURIComponent(value || ""); } function parseValue(flagName, value) { value = value.toLowerCase(); if (value === "true" || value === "false") { return value === "true"; } else if ("" + +value === value) { return +value; } throw new Error("Could not parse value flag value " + value + " for flag " + flagName + "."); } function env3() { return exports2.ENV; } exports2.ENV = null; function setEnvironmentGlobal(environment) { exports2.ENV = environment; } /** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ var globalNameSpace; function getGlobalNamespace() { if (globalNameSpace == null) { var ns = void 0; if (typeof window !== "undefined") { ns = window; } else if (typeof global !== "undefined") { ns = global; } else if (typeof process !== "undefined") { ns = process; } else if (typeof self !== "undefined") { ns = self; } else { throw new Error("Could not find a global object"); } globalNameSpace = ns; } return globalNameSpace; } function getGlobalMap() { var ns = getGlobalNamespace(); if (ns._tfGlobals == null) { ns._tfGlobals = new Map(); } return ns._tfGlobals; } function getGlobal(key, init2) { var globalMap = getGlobalMap(); if (globalMap.has(key)) { return globalMap.get(key); } else { var singleton = init2(); globalMap.set(key, singleton); return globalMap.get(key); } } var Abs3 = "Abs"; var Acos = "Acos"; var Acosh = "Acosh"; var Add3 = "Add"; var AddN3 = "AddN"; var All = "All"; var Any = "Any"; var ArgMax3 = "ArgMax"; var ArgMin = "ArgMin"; var Asin = "Asin"; var Asinh = "Asinh"; var Atan = "Atan"; var Atanh = "Atanh"; var Atan2 = "Atan2"; var AvgPool3 = "AvgPool"; var AvgPoolBackprop = "AvgPoolBackprop"; var AvgPool3D = "AvgPool3D"; var AvgPool3DBackprop = "AvgPool3DBackprop"; var BatchMatMul3 = "BatchMatMul"; var BatchToSpaceND = "BatchToSpaceND"; var BroadcastTo = "BroadcastTo"; var Cast5 = "Cast"; var Ceil = "Ceil"; var ClipByValue3 = "ClipByValue"; var Complex = "Complex"; var Concat3 = "Concat"; var Conv2D3 = "Conv2D"; var Conv2DBackpropFilter = "Conv2DBackpropFilter"; var Conv2DBackpropInput3 = "Conv2DBackpropInput"; var Conv3D = "Conv3D"; var Conv3DBackpropFilterV2 = "Conv3DBackpropFilterV2"; var Conv3DBackpropInputV2 = "Conv3DBackpropInputV2"; var Cos3 = "Cos"; var Cosh = "Cosh"; var Cumsum3 = "Cumsum"; var CropAndResize3 = "CropAndResize"; var DepthToSpace3 = "DepthToSpace"; var DepthwiseConv2dNative3 = "DepthwiseConv2dNative"; var DepthwiseConv2dNativeBackpropFilter = "DepthwiseConv2dNativeBackpropFilter"; var DepthwiseConv2dNativeBackpropInput = "DepthwiseConv2dNativeBackpropInput"; var Diag = "Diag"; var Dilation2D = "Dilation2D"; var Dilation2DBackpropInput = "Dilation2DBackpropInput"; var Dilation2DBackpropFilter = "Dilation2DBackpropFilter"; var Div3 = "Div"; var Elu = "Elu"; var EluGrad = "EluGrad"; var Erf = "Erf"; var Equal3 = "Equal"; var Exp3 = "Exp"; var Expm1 = "Expm1"; var FFT = "FFT"; var Fill3 = "Fill"; var FlipLeftRight3 = "FlipLeftRight"; var Floor = "Floor"; var FloorDiv3 = "FloorDiv"; var FusedBatchNorm3 = "FusedBatchNorm"; var GatherV23 = "GatherV2"; var GatherNd3 = "GatherNd"; var Greater3 = "Greater"; var GreaterEqual3 = "GreaterEqual"; var Identity5 = "Identity"; var IFFT = "IFFT"; var Imag = "Imag"; var IsFinite = "IsFinite"; var IsInf = "IsInf"; var IsNan = "IsNan"; var Less3 = "Less"; var LessEqual3 = "LessEqual"; var LinSpace = "LinSpace"; var Log3 = "Log"; var Log1p = "Log1p"; var LogicalAnd3 = "LogicalAnd"; var LogicalNot = "LogicalNot"; var LogicalOr = "LogicalOr"; var LogSoftmax = "LogSoftmax"; var LRN = "LRN"; var LRNBackprop = "LRNBackprop"; var Max3 = "Max"; var Maximum3 = "Maximum"; var MaxPool3 = "MaxPool"; var MaxPoolBackprop = "MaxPoolBackprop"; var MaxPool3D = "MaxPool3D"; var MaxPool3DBackprop = "MaxPool3DBackprop"; var MaxPoolWithArgmax = "MaxPoolWithArgmax"; var Mean = "Mean"; var Min3 = "Min"; var Minimum3 = "Minimum"; var MirrorPad = "MirrorPad"; var Mod = "Mod"; var Multiply3 = "Multiply"; var Negate3 = "Negate"; var NotEqual3 = "NotEqual"; var NonMaxSuppressionV33 = "NonMaxSuppressionV3"; var NonMaxSuppressionV43 = "NonMaxSuppressionV4"; var NonMaxSuppressionV53 = "NonMaxSuppressionV5"; var OnesLike3 = "OnesLike"; var OneHot3 = "OneHot"; var PadV23 = "PadV2"; var Pool = "Pool"; var Pow3 = "Pow"; var Prelu3 = "Prelu"; var Prod = "Prod"; var Range = "Range"; var Real = "Real"; var Reciprocal = "Reciprocal"; var Relu3 = "Relu"; var Reshape6 = "Reshape"; var ResizeNearestNeighbor = "ResizeNearestNeighbor"; var ResizeNearestNeighborGrad = "ResizeNearestNeighborGrad"; var ResizeBilinear3 = "ResizeBilinear"; var ResizeBilinearGrad = "ResizeBilinearGrad"; var Relu63 = "Relu6"; var Reverse3 = "Reverse"; var Round = "Round"; var Rsqrt3 = "Rsqrt"; var ScatterNd3 = "ScatterNd"; var SelectV23 = "SelectV2"; var Selu = "Selu"; var Slice6 = "Slice"; var Sin3 = "Sin"; var Sinh = "Sinh"; var Sign = "Sign"; var Sigmoid3 = "Sigmoid"; var Softplus = "Softplus"; var Sqrt3 = "Sqrt"; var Sum3 = "Sum"; var SpaceToBatchND = "SpaceToBatchND"; var SplitV2 = "SplitV"; var Softmax3 = "Softmax"; var SquaredDifference3 = "SquaredDifference"; var Square3 = "Square"; var Sub3 = "Sub"; var SparseToDense = "SparseToDense"; var StridedSlice3 = "StridedSlice"; var Tan = "Tan"; var Tanh3 = "Tanh"; var Tile3 = "Tile"; var TopK = "TopK"; var Transpose5 = "Transpose"; var Unique = "Unique"; var Unpack3 = "Unpack"; var UnsortedSegmentSum = "UnsortedSegmentSum"; var ZerosLike3 = "ZerosLike"; var Step = "Step"; var FromPixels = "FromPixels"; var RotateWithOffset3 = "RotateWithOffset"; var _FusedMatMul2 = "_FusedMatMul"; var FusedConv2D3 = "FusedConv2D"; var FusedDepthwiseConv2D3 = "FusedDepthwiseConv2D"; /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ var kernelRegistry = getGlobal("kernelRegistry", function() { return new Map(); }); var gradRegistry = getGlobal("gradRegistry", function() { return new Map(); }); function getKernel(kernelName, backendName) { var key = makeKey(kernelName, backendName); return kernelRegistry.get(key); } function getGradient(kernelName) { return gradRegistry.get(kernelName); } function getKernelsForBackend(backendName) { var it = kernelRegistry.entries(); var result = []; while (true) { var _a = it.next(), done = _a.done, value = _a.value; if (done) { break; } var key = value[0], config2 = value[1]; var backend2 = key.split("_")[0]; if (backend2 === backendName) { result.push(config2); } } return result; } function registerKernel2(config2) { var kernelName = config2.kernelName, backendName = config2.backendName; var key = makeKey(kernelName, backendName); if (kernelRegistry.has(key)) { console.warn("The kernel '" + kernelName + "' for backend " + ("'" + backendName + "' is already registered")); } kernelRegistry.set(key, config2); } function registerGradient(config2) { var kernelName = config2.kernelName; if (gradRegistry.has(kernelName)) { if (env3().getBool("DEBUG")) { console.warn("Overriding the gradient for '" + kernelName + "'"); } } gradRegistry.set(kernelName, config2); } function unregisterKernel(kernelName, backendName) { var key = makeKey(kernelName, backendName); if (!kernelRegistry.has(key)) { throw new Error("The kernel '" + kernelName + "' for backend " + ("'" + backendName + "' is not registered")); } kernelRegistry.delete(key); } function unregisterGradient(kernelName) { if (!gradRegistry.has(kernelName)) { throw new Error("The gradient '" + kernelName + "' for backend is not registered"); } gradRegistry.delete(kernelName); } function copyRegisteredKernels(registeredBackendName, newBackendName) { var kernels = getKernelsForBackend(registeredBackendName); kernels.forEach(function(kernelConfig) { var newKernelConfig = Object.assign({}, kernelConfig, {backendName: newBackendName}); registerKernel2(newKernelConfig); }); } function makeKey(kernelName, backendName) { return backendName + "_" + kernelName; } /** * @license * Copyright 2017 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function createScalarValue(value, dtype) { if (dtype === "string") { return encodeString(value); } return toTypedArray([value], dtype); } function noConversionNeeded(a, dtype) { return a instanceof Float32Array && dtype === "float32" || a instanceof Int32Array && dtype === "int32" || a instanceof Uint8Array && dtype === "bool"; } function toTypedArray(a, dtype) { if (dtype === "string") { throw new Error("Cannot convert a string[] to a TypedArray"); } if (Array.isArray(a)) { a = flatten(a); } if (env3().getBool("DEBUG")) { checkConversionForErrors(a, dtype); } if (noConversionNeeded(a, dtype)) { return a; } if (dtype == null || dtype === "float32" || dtype === "complex64") { return new Float32Array(a); } else if (dtype === "int32") { return new Int32Array(a); } else if (dtype === "bool") { var bool = new Uint8Array(a.length); for (var i = 0; i < bool.length; ++i) { if (Math.round(a[i]) !== 0) { bool[i] = 1; } } return bool; } else { throw new Error("Unknown data type " + dtype); } } function now2() { return env3().platform.now(); } function fetch$1(path, requestInits) { return env3().platform.fetch(path, requestInits); } function encodeString(s, encoding) { if (encoding === void 0) { encoding = "utf-8"; } encoding = encoding || "utf-8"; return env3().platform.encode(s, encoding); } function decodeString(bytes, encoding) { if (encoding === void 0) { encoding = "utf-8"; } encoding = encoding || "utf-8"; return env3().platform.decode(bytes, encoding); } var util27 = { __proto__: null, createScalarValue, toTypedArray, now: now2, fetch: fetch$1, encodeString, decodeString, shuffle, clamp, nearestLargerEven, sum: sum2, randUniform, distSquared, assert, assertShapesMatch, assertNonNull, flatten, sizeFromShape, isScalarShape, arraysEqual, isInt, tanh, sizeToSquarishShape, createShuffledIndices, rightPad, repeatedTry, inferFromImplicitShape, parseAxisParam, squeezeShape, getTypedArrayFromDType, getArrayFromDType, checkConversionForErrors, isValidDtype, hasEncodingLoss, isTypedArray, bytesPerElement, bytesFromStringArray, isString, isBoolean, isNumber, inferDtype, isFunction, nearestDivisor, computeStrides, toNestedArray, makeOnesTypedArray, makeZerosTypedArray, makeZerosNestedTypedArray, assertNonNegativeIntegerDimensions, locToIndex, indexToLoc, isPromise }; /** * @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 Profiler = function() { function Profiler2(backendTimer, logger) { this.backendTimer = backendTimer; this.logger = logger; if (logger == null) { this.logger = new Logger(); } } Profiler2.prototype.profileKernel = function(kernelName, inputs, f) { var outputs; var holdResultWrapperFn = function() { outputs = f(); }; var timer = this.backendTimer.time(holdResultWrapperFn); var _loop_1 = function(i2) { var output = outputs[i2]; output.data().then(function(tensorVals) { checkComputationForErrors(tensorVals, output.dtype, kernelName); }); }; for (var i = 0; i < outputs.length; i++) { _loop_1(i); } var kernelProfile = { kernelName, outputs, inputs, timeMs: timer.then(function(timing) { return timing.kernelMs; }), extraInfo: timer.then(function(timing) { return timing.getExtraProfileInfo != null ? timing.getExtraProfileInfo() : ""; }) }; return kernelProfile; }; Profiler2.prototype.logKernelProfile = function(kernelProfile) { var _this = this; var kernelName = kernelProfile.kernelName, outputs = kernelProfile.outputs, timeMs = kernelProfile.timeMs, inputs = kernelProfile.inputs, extraInfo = kernelProfile.extraInfo; outputs.forEach(function(result) { Promise.all([result.data(), timeMs, extraInfo]).then(function(valueContainer) { _this.logger.logKernelProfile(kernelName, result, valueContainer[0], valueContainer[1], inputs, valueContainer[2]); }); }); }; return Profiler2; }(); function checkComputationForErrors(vals, dtype, kernelName) { if (dtype !== "float32") { return false; } for (var i = 0; i < vals.length; i++) { var num = vals[i]; if (isNaN(num) || !isFinite(num)) { console.warn("Found " + num + " in the result of '" + kernelName + "'"); return true; } } return false; } var Logger = function() { function Logger2() { } Logger2.prototype.logKernelProfile = function(name, result, vals, timeMs, inputs, extraInfo) { var time2 = typeof timeMs === "number" ? rightPad(timeMs + "ms", 9) : timeMs["error"]; var paddedName = rightPad(name, 25); var rank = result.rank; var size = result.size; var shape = rightPad(result.shape.toString(), 14); var inputShapesDescription = ""; for (var name_1 in inputs) { var input = inputs[name_1]; if (input != null) { var inputShape = input.shape || result.shape; var inputRank = inputShape.length; inputShapesDescription += name_1 + ": " + inputRank + "D " + (inputRank > 0 ? inputShape : "") + " "; } } console.log("%c" + paddedName + " %c" + time2 + " %c" + rank + "D " + shape + " %c" + size + " %c" + inputShapesDescription + " %c" + extraInfo, "font-weight:bold", "color:red", "color:blue", "color: orange", "color: green", "color: steelblue"); }; return Logger2; }(); /** * @license * Copyright 2017 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ 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]) { node.outputs.forEach(function(output) { return tensorsFromX[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; for (var j = 0; j < node.outputs.length; j++) { if (tensorsLeadToY[node.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.outputs = node.outputs; filteredTape.push(prunedNode); } } return filteredTape; } function backpropagateGradients(tensorAccumulatedGradientMap, filteredTape, tidy2, add2) { var _loop_1 = function(i2) { var node = filteredTape[i2]; var dys = []; node.outputs.forEach(function(o) { var gradTensor = tensorAccumulatedGradientMap[o.id]; if (gradTensor != null) { dys.push(gradTensor); } else { dys.push(null); } }); if (node.gradient == null) { throw new Error("Cannot compute gradient: gradient function not found " + ("for " + node.kernelName + ".")); } var inputGradients = node.gradient(dys); var _loop_2 = function(inputName2) { if (!(inputName2 in inputGradients)) { throw new Error("Cannot backprop through input " + inputName2 + ". " + ("Available gradients found: " + Object.keys(inputGradients) + ".")); } var dx = tidy2(function() { return inputGradients[inputName2](); }); if (dx.dtype !== "float32") { throw new Error("Error in gradient for op " + node.kernelName + ". The gradient of input " + (inputName2 + " must have 'float32' dtype, but has '" + dx.dtype + "'")); } var x = node.inputs[inputName2]; if (!arraysEqual(dx.shape, x.shape)) { throw new Error("Error in gradient for op " + node.kernelName + ". The gradient of input " + ("'" + inputName2 + "' 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] = add2(curGradient, dx); curGradient.dispose(); } }; for (var inputName in node.inputs) { _loop_2(inputName); } }; for (var i = filteredTape.length - 1; i >= 0; i--) { _loop_1(i); } } /** * @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 FORMAT_LIMIT_NUM_VALS = 20; var FORMAT_NUM_FIRST_LAST_VALS = 3; var FORMAT_NUM_SIG_DIGITS = 7; function tensorToString(vals, shape, dtype, verbose) { var strides = computeStrides(shape); var padPerCol = computeMaxSizePerColumn(vals, shape, dtype, strides); var rank = shape.length; var valsLines = subTensorToString(vals, shape, dtype, strides, padPerCol); var lines = ["Tensor"]; if (verbose) { lines.push(" dtype: " + dtype); lines.push(" rank: " + rank); lines.push(" shape: [" + shape + "]"); lines.push(" values:"); } lines.push(valsLines.map(function(l) { return " " + l; }).join("\n")); return lines.join("\n"); } function computeMaxSizePerColumn(vals, shape, dtype, strides) { var n = sizeFromShape(shape); var numCols = strides[strides.length - 1]; var padPerCol = new Array(numCols).fill(0); var rank = shape.length; var valuesOrTuples = dtype === "complex64" ? createComplexTuples(vals) : vals; if (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(valuesOrTuples[offset + j], 0, dtype).length); } } } return padPerCol; } function valToString(val, pad3, dtype) { var valStr; if (Array.isArray(val)) { valStr = parseFloat(val[0].toFixed(FORMAT_NUM_SIG_DIGITS)) + " + " + (parseFloat(val[1].toFixed(FORMAT_NUM_SIG_DIGITS)) + "j"); } else if (isString(val)) { valStr = "'" + val + "'"; } else if (dtype === "bool") { valStr = boolNumToString(val); } else { valStr = parseFloat(val.toFixed(FORMAT_NUM_SIG_DIGITS)).toString(); } return rightPad(valStr, pad3); } function boolNumToString(v) { return v === 0 ? "false" : "true"; } function subTensorToString(vals, shape, dtype, strides, padPerCol, isLast) { if (isLast === void 0) { isLast = true; } var storagePerElement = dtype === "complex64" ? 2 : 1; var size = shape[0]; var rank = shape.length; if (rank === 0) { if (dtype === "complex64") { var complexTuple = createComplexTuples(vals); return [valToString(complexTuple[0], 0, dtype)]; } if (dtype === "bool") { return [boolNumToString(vals[0])]; } return [vals[0].toString()]; } if (rank === 1) { if (size > FORMAT_LIMIT_NUM_VALS) { var firstValsSize = FORMAT_NUM_FIRST_LAST_VALS * storagePerElement; var firstVals = Array.from(vals.slice(0, firstValsSize)); var lastVals = Array.from(vals.slice((size - FORMAT_NUM_FIRST_LAST_VALS) * storagePerElement, size * storagePerElement)); if (dtype === "complex64") { firstVals = createComplexTuples(firstVals); lastVals = createComplexTuples(lastVals); } return [ "[" + firstVals.map(function(x, i2) { return valToString(x, padPerCol[i2], dtype); }).join(", ") + ", ..., " + lastVals.map(function(x, i2) { return valToString(x, padPerCol[size - FORMAT_NUM_FIRST_LAST_VALS + i2], dtype); }).join(", ") + "]" ]; } var displayVals = dtype === "complex64" ? createComplexTuples(vals) : Array.from(vals); return [ "[" + displayVals.map(function(x, i2) { return valToString(x, padPerCol[i2], dtype); }).join(", ") + "]" ]; } var subshape = shape.slice(1); var substrides = strides.slice(1); var stride = strides[0] * storagePerElement; 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.slice(start, end), subshape, dtype, 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.slice(start, end), subshape, dtype, 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.slice(start, end), subshape, dtype, 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 createComplexTuples(vals) { var complexTuples = []; for (var i = 0; i < vals.length; i += 2) { complexTuples.push([vals[i], vals[i + 1]]); } return complexTuples; } /** * @license * Copyright 2017 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ var TensorBuffer = function() { function TensorBuffer2(shape, dtype, values) { var _this = this; this.dtype = dtype; this.shape = shape.slice(); this.size = sizeFromShape(shape); if (values != null) { var n_1 = values.length; assert(n_1 === this.size, function() { return "Length of values '" + n_1 + "' does not match the size " + ("inferred by the shape '" + _this.size + "'."); }); } if (dtype === "complex64") { throw new Error("complex64 dtype TensorBuffers are not supported. Please create a TensorBuffer for the real and imaginary parts separately and call tf.complex(real, imag)."); } this.values = values || getArrayFromDType(dtype, this.size); this.strides = computeStrides(shape); } TensorBuffer2.prototype.set = function(value) { var _this = this; var locs = []; for (var _i2 = 1; _i2 < arguments.length; _i2++) { locs[_i2 - 1] = arguments[_i2]; } if (locs.length === 0) { locs = [0]; } assert(locs.length === this.rank, function() { return "The number of provided coordinates (" + locs.length + ") must " + ("match the rank (" + _this.rank + ")"); }); var index = this.locToIndex(locs); this.values[index] = value; }; TensorBuffer2.prototype.get = function() { var locs = []; for (var _i2 = 0; _i2 < arguments.length; _i2++) { locs[_i2] = arguments[_i2]; } if (locs.length === 0) { locs = [0]; } var i = 0; for (var _a = 0, locs_1 = locs; _a < locs_1.length; _a++) { var loc = locs_1[_a]; if (loc < 0 || loc >= this.shape[i]) { var msg = "Requested out of range element at " + locs + ". " + (" Buffer shape=" + this.shape); throw new Error(msg); } i++; } var index = locs[locs.length - 1]; for (var i_1 = 0; i_1 < locs.length - 1; ++i_1) { index += this.strides[i_1] * locs[i_1]; } return this.values[index]; }; TensorBuffer2.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; }; TensorBuffer2.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(TensorBuffer2.prototype, "rank", { get: function() { return this.shape.length; }, enumerable: true, configurable: true }); TensorBuffer2.prototype.toTensor = function() { return trackerFn().makeTensor(this.values, this.shape, this.dtype); }; return TensorBuffer2; }(); var trackerFn = null; var opHandler = null; function setTensorTracker(fn) { trackerFn = fn; } function setOpHandler(handler) { opHandler = handler; } var Tensor = function() { function Tensor2(shape, dtype, dataId, id) { this.kept = false; this.isDisposedInternal = false; this.shape = shape.slice(); this.dtype = dtype || "float32"; this.size = sizeFromShape(shape); this.strides = computeStrides(shape); this.dataId = dataId; this.id = id; this.rankType = this.rank < 5 ? this.rank.toString() : "higher"; } Object.defineProperty(Tensor2.prototype, "rank", { get: function() { return this.shape.length; }, enumerable: true, configurable: true }); Tensor2.prototype.buffer = function() { return __awaiter(this, void 0, void 0, function() { var vals; return __generator(this, function(_a) { switch (_a.label) { case 0: return [4, this.data()]; case 1: vals = _a.sent(); return [2, opHandler.buffer(this.shape, this.dtype, vals)]; } }); }); }; Tensor2.prototype.bufferSync = function() { return opHandler.buffer(this.shape, this.dtype, this.dataSync()); }; Tensor2.prototype.array = function() { return __awaiter(this, void 0, void 0, function() { var vals; return __generator(this, function(_a) { switch (_a.label) { case 0: return [4, this.data()]; case 1: vals = _a.sent(); return [2, toNestedArray(this.shape, vals)]; } }); }); }; Tensor2.prototype.arraySync = function() { return toNestedArray(this.shape, this.dataSync()); }; Tensor2.prototype.data = function() { return __awaiter(this, void 0, void 0, function() { var data2, bytes; return __generator(this, function(_a) { switch (_a.label) { case 0: this.throwIfDisposed(); data2 = trackerFn().read(this.dataId); if (!(this.dtype === "string")) return [3, 2]; return [4, data2]; case 1: bytes = _a.sent(); try { return [2, bytes.map(function(b) { return decodeString(b); })]; } catch (_b) { throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes()."); } _a.label = 2; case 2: return [2, data2]; } }); }); }; Tensor2.prototype.dataSync = function() { this.throwIfDisposed(); var data2 = trackerFn().readSync(this.dataId); if (this.dtype === "string") { try { return data2.map(function(b) { return decodeString(b); }); } catch (_a) { throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes()."); } } return data2; }; Tensor2.prototype.bytes = function() { return __awaiter(this, void 0, void 0, function() { var data2; return __generator(this, function(_a) { switch (_a.label) { case 0: this.throwIfDisposed(); return [4, trackerFn().read(this.dataId)]; case 1: data2 = _a.sent(); if (this.dtype === "string") { return [2, data2]; } else { return [2, new Uint8Array(data2.buffer)]; } } }); }); }; Tensor2.prototype.dispose = function() { if (this.isDisposed) { return; } trackerFn().disposeTensor(this); this.isDisposedInternal = true; }; Object.defineProperty(Tensor2.prototype, "isDisposed", { get: function() { return this.isDisposedInternal; }, enumerable: true, configurable: true }); Tensor2.prototype.throwIfDisposed = function() { if (this.isDisposed) { throw new Error("Tensor is disposed."); } }; Tensor2.prototype.print = function(verbose) { if (verbose === void 0) { verbose = false; } return opHandler.print(this, verbose); }; Tensor2.prototype.clone = function() { this.throwIfDisposed(); return opHandler.clone(this); }; Tensor2.prototype.toString = function(verbose) { if (verbose === void 0) { verbose = false; } var vals = this.dataSync(); return tensorToString(vals, this.shape, this.dtype, verbose); }; Tensor2.prototype.cast = function(dtype) { this.throwIfDisposed(); return opHandler.cast(this, dtype); }; Tensor2.prototype.variable = function(trainable, name, dtype) { if (trainable === void 0) { trainable = true; } this.throwIfDisposed(); return trackerFn().makeVariable(this, trainable, name, dtype); }; return Tensor2; }(); Object.defineProperty(Tensor, Symbol.hasInstance, { value: function(instance) { return !!instance && instance.data != null && instance.dataSync != null && instance.throwIfDisposed != null; } }); var Variable = function(_super) { __extends(Variable2, _super); function Variable2(initialValue, trainable, name, tensorId) { var _this = _super.call(this, initialValue.shape, initialValue.dtype, initialValue.dataId, tensorId) || this; _this.trainable = trainable; _this.name = name; return _this; } Variable2.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")); } trackerFn().disposeTensor(this); this.dataId = newValue.dataId; trackerFn().incRef(this, null); }; Variable2.prototype.dispose = function() { trackerFn().disposeVariable(this); this.isDisposedInternal = true; }; return Variable2; }(Tensor); Object.defineProperty(Variable, Symbol.hasInstance, { value: function(instance) { return instance instanceof Tensor && instance.assign != null && instance.assign instanceof Function; } }); /** * @license * Copyright 2017 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ (function(Rank) { Rank["R0"] = "R0"; Rank["R1"] = "R1"; Rank["R2"] = "R2"; Rank["R3"] = "R3"; Rank["R4"] = "R4"; Rank["R5"] = "R5"; Rank["R6"] = "R6"; })(exports2.Rank || (exports2.Rank = {})); var UpcastInt32AndMap; (function(UpcastInt32AndMap2) { UpcastInt32AndMap2["float32"] = "float32"; UpcastInt32AndMap2["int32"] = "int32"; UpcastInt32AndMap2["bool"] = "int32"; UpcastInt32AndMap2["complex64"] = "complex64"; })(UpcastInt32AndMap || (UpcastInt32AndMap = {})); var UpcastBoolAndMap; (function(UpcastBoolAndMap2) { UpcastBoolAndMap2["float32"] = "float32"; UpcastBoolAndMap2["int32"] = "int32"; UpcastBoolAndMap2["bool"] = "bool"; UpcastBoolAndMap2["complex64"] = "complex64"; })(UpcastBoolAndMap || (UpcastBoolAndMap = {})); var UpcastFloat32AndMap; (function(UpcastFloat32AndMap2) { UpcastFloat32AndMap2["float32"] = "float32"; UpcastFloat32AndMap2["int32"] = "float32"; UpcastFloat32AndMap2["bool"] = "float32"; UpcastFloat32AndMap2["complex64"] = "complex64"; })(UpcastFloat32AndMap || (UpcastFloat32AndMap = {})); var UpcastComplex64AndMap; (function(UpcastComplex64AndMap2) { UpcastComplex64AndMap2["float32"] = "complex64"; UpcastComplex64AndMap2["int32"] = "complex64"; UpcastComplex64AndMap2["bool"] = "complex64"; UpcastComplex64AndMap2["complex64"] = "complex64"; })(UpcastComplex64AndMap || (UpcastComplex64AndMap = {})); var upcastTypeMap = { float32: UpcastFloat32AndMap, int32: UpcastInt32AndMap, bool: UpcastBoolAndMap, complex64: UpcastComplex64AndMap }; function upcastType(typeA, typeB) { if (typeA === "string" || typeB === "string") { if (typeA === "string" && typeB === "string") { return "string"; } throw new Error("Can not upcast " + typeA + " with " + typeB); } return upcastTypeMap[typeA][typeB]; } function sumOutType(type) { return upcastType(type, "int32"); } /** * @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. * ============================================================================= */ function makeTypesMatch(a, b) { if (a.dtype === b.dtype) { return [a, b]; } var dtype = upcastType(a.dtype, b.dtype); return [a.cast(dtype), b.cast(dtype)]; } function assertTypesMatch(a, b) { assert(a.dtype === b.dtype, function() { return "The dtypes of the first(" + a.dtype + ") and" + (" second(" + b.dtype + ") input must match"); }); } function isTensorInList(tensor2, tensorList) { return tensorList.some(function(x) { return x.id === tensor2.id; }); } function getTensorsInContainer(result) { var list = []; var seen = new Set(); walkTensorContainer(result, list, seen); return list; } function walkTensorContainer(container, list, seen) { if (container == null) { return; } if (container instanceof Tensor) { list.push(container); return; } if (!isIterable(container)) { return; } var iterable = container; for (var k in iterable) { var val = iterable[k]; if (!seen.has(val)) { seen.add(val); walkTensorContainer(val, list, seen); } } } function isIterable(obj) { return Array.isArray(obj) || typeof obj === "object"; } var tensor_util = { __proto__: null, makeTypesMatch, assertTypesMatch, isTensorInList, getTensorsInContainer }; /** * @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 EngineState = function() { function EngineState2() { this.registeredVariables = {}; this.nextTapeNodeId = 0; this.numBytes = 0; this.numTensors = 0; this.numStringTensors = 0; this.numDataBuffers = 0; this.gradientDepth = 0; this.kernelDepth = 0; this.scopeStack = []; this.numDataMovesStack = []; this.nextScopeId = 0; this.tensorInfo = new WeakMap(); this.profiling = false; this.activeProfile = {newBytes: 0, newTensors: 0, peakBytes: 0, kernels: [], result: null}; } EngineState2.prototype.dispose = function() { for (var variableName in this.registeredVariables) { this.registeredVariables[variableName].dispose(); } }; return EngineState2; }(); var Engine = function() { function Engine2(ENV3) { this.ENV = ENV3; this.registry = {}; this.registryFactory = {}; this.pendingBackendInitId = 0; this.state = new EngineState(); } Engine2.prototype.ready = function() { return __awaiter(this, void 0, void 0, function() { var sortedBackends, i, backendName, success; return __generator(this, function(_a) { switch (_a.label) { case 0: if (this.pendingBackendInit != null) { return [2, this.pendingBackendInit.then(function() { })]; } if (this.backendInstance != null) { return [2]; } sortedBackends = this.getSortedBackends(); i = 0; _a.label = 1; case 1: if (!(i < sortedBackends.length)) return [3, 5]; backendName = sortedBackends[i]; return [4, this.initializeBackend(backendName).success]; case 2: success = _a.sent(); if (!success) return [3, 4]; return [4, this.setBackend(backendName)]; case 3: _a.sent(); return [2]; case 4: i++; return [3, 1]; case 5: throw new Error("Could not initialize any backends, all backend initializations failed."); } }); }); }; Object.defineProperty(Engine2.prototype, "backend", { get: function() { if (this.pendingBackendInit != null) { throw new Error("Backend '" + this.backendName + "' has not yet been initialized. Make sure to await tf.ready() or await tf.setBackend() before calling other methods"); } if (this.backendInstance == null) { var _a = this.initializeBackendsAndReturnBest(), name_1 = _a.name, asyncInit = _a.asyncInit; if (asyncInit) { throw new Error("The highest priority backend '" + name_1 + "' has not yet been initialized. Make sure to await tf.ready() or await tf.setBackend() before calling other methods"); } this.setBackend(name_1); } return this.backendInstance; }, enumerable: true, configurable: true }); Engine2.prototype.backendNames = function() { return Object.keys(this.registryFactory); }; Engine2.prototype.findBackend = function(backendName) { if (!(backendName in this.registry)) { if (backendName in this.registryFactory) { var asyncInit = this.initializeBackend(backendName).asyncInit; if (asyncInit) { return null; } } else { return null; } } return this.registry[backendName]; }; Engine2.prototype.findBackendFactory = function(backendName) { if (!(backendName in this.registryFactory)) { return null; } return this.registryFactory[backendName].factory; }; Engine2.prototype.registerBackend = function(backendName, factory, priority) { if (priority === void 0) { priority = 1; } if (backendName in this.registryFactory) { console.warn(backendName + " backend was already registered. Reusing existing backend factory."); return false; } this.registryFactory[backendName] = {factory, priority}; return true; }; Engine2.prototype.setBackend = function(backendName) { return __awaiter(this, void 0, void 0, function() { var _a, success, asyncInit, result, _b; return __generator(this, function(_c) { switch (_c.label) { case 0: if (this.registryFactory[backendName] == null) { throw new Error("Backend name '" + backendName + "' not found in registry"); } this.backendName = backendName; if (!(this.registry[backendName] == null)) return [3, 4]; this.backendInstance = null; _a = this.initializeBackend(backendName), success = _a.success, asyncInit = _a.asyncInit; if (!asyncInit) return [3, 2]; return [4, success]; case 1: _b = _c.sent(); return [3, 3]; case 2: _b = success; _c.label = 3; case 3: result = _b; if (!result) { return [2, false]; } _c.label = 4; case 4: this.backendInstance = this.registry[backendName]; this.setupRegisteredKernels(); this.profiler = new Profiler(this.backendInstance); return [2, true]; } }); }); }; Engine2.prototype.setupRegisteredKernels = function() { var _this = this; var kernels = getKernelsForBackend(this.backendName); kernels.forEach(function(kernel) { if (kernel.setupFunc != null) { kernel.setupFunc(_this.backendInstance); } }); }; Engine2.prototype.disposeRegisteredKernels = function(backendName) { var _this = this; var kernels = getKernelsForBackend(backendName); kernels.forEach(function(kernel) { if (kernel.disposeFunc != null) { kernel.disposeFunc(_this.registry[backendName]); } }); }; Engine2.prototype.initializeBackend = function(backendName) { var _this = this; var registryFactoryEntry = this.registryFactory[backendName]; if (registryFactoryEntry == null) { throw new Error("Cannot initialize backend " + backendName + ", no registration found."); } try { var backend2 = registryFactoryEntry.factory(); if (backend2 && !(backend2 instanceof KernelBackend2) && typeof backend2.then === "function") { var promiseId_1 = ++this.pendingBackendInitId; var success = backend2.then(function(backendInstance) { if (promiseId_1 < _this.pendingBackendInitId) { return false; } _this.registry[backendName] = backendInstance; _this.pendingBackendInit = null; return true; }).catch(function(err) { if (promiseId_1 < _this.pendingBackendInitId) { return false; } _this.pendingBackendInit = null; console.warn("Initialization of backend " + backendName + " failed"); console.warn(err.stack || err.message); return false; }); this.pendingBackendInit = success; return {success, asyncInit: true}; } else { this.registry[backendName] = backend2; return {success: true, asyncInit: false}; } } catch (err) { console.warn("Initialization of backend " + backendName + " failed"); console.warn(err.stack || err.message); return {success: false, asyncInit: false}; } }; Engine2.prototype.removeBackend = function(backendName) { if (!(backendName in this.registryFactory)) { throw new Error(backendName + " backend not found in registry"); } if (this.backendName === backendName && this.pendingBackendInit != null) { this.pendingBackendInitId++; } if (backendName in this.registry) { this.disposeRegisteredKernels(backendName); this.registry[backendName].dispose(); delete this.registry[backendName]; } delete this.registryFactory[backendName]; if (this.backendName === backendName) { this.pendingBackendInit = null; this.backendName = null; this.backendInstance = null; } }; Engine2.prototype.getSortedBackends = function() { var _this = this; if (Object.keys(this.registryFactory).length === 0) { throw new Error("No backend found in registry."); } return Object.keys(this.registryFactory).sort(function(a, b) { return _this.registryFactory[b].priority - _this.registryFactory[a].priority; }); }; Engine2.prototype.initializeBackendsAndReturnBest = function() { var sortedBackends = this.getSortedBackends(); for (var i = 0; i < sortedBackends.length; i++) { var backendName = sortedBackends[i]; var _a = this.initializeBackend(backendName), success = _a.success, asyncInit = _a.asyncInit; if (asyncInit || success) { return {name: backendName, asyncInit}; } } throw new Error("Could not initialize any backends, all backend initializations failed."); }; Engine2.prototype.moveData = function(backend2, dataId) { var info = this.state.tensorInfo.get(dataId); var srcBackend = info.backend; var values = this.readSync(dataId); srcBackend.disposeData(dataId); info.backend = backend2; backend2.move(dataId, values, info.shape, info.dtype); if (this.shouldCheckForMemLeaks()) { this.state.numDataMovesStack[this.state.numDataMovesStack.length - 1]++; } }; Engine2.prototype.tidy = function(nameOrFn, fn) { var _this = this; 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; } var result; return this.scopedRun(function() { return _this.startScope(name); }, function() { return _this.endScope(result); }, function() { result = fn(); if (result instanceof Promise) { console.error("Cannot return a Promise inside of tidy."); } return result; }); }; Engine2.prototype.scopedRun = function(start, end, f) { start(); try { var res = f(); end(); return res; } catch (ex) { end(); throw ex; } }; Engine2.prototype.nextTensorId = function() { return Engine2.nextTensorId++; }; Engine2.prototype.nextVariableId = function() { return Engine2.nextVariableId++; }; Engine2.prototype.clone = function(x) { var y = this.makeTensorFromDataId(x.dataId, x.shape, x.dtype); var inputs = {x}; var grad2 = function(dy) { return { x: function() { var dtype = "float32"; var gradInputs = {x: dy}; var attrs = {dtype}; return ENGINE.runKernelFunc(function(backend2) { return backend2.cast(dy, dtype); }, gradInputs, null, Cast5, attrs); } }; }; var saved = []; this.addTapeNode(this.state.activeScope.name, inputs, [y], grad2, saved, {}); return y; }; Engine2.prototype.runKernel = function(kernelName, inputs, attrs, inputsToSave, outputsToSave) { var forwardFunc = null; var backwardsFunc = null; return this.runKernelFunc(forwardFunc, inputs, backwardsFunc, kernelName, attrs, inputsToSave, outputsToSave); }; Engine2.prototype.shouldCheckForMemLeaks = function() { return this.ENV.getBool("IS_TEST"); }; Engine2.prototype.checkKernelForMemLeak = function(kernelName, numDataIdsBefore, outInfos) { var numDataIdsAfter = this.backend.numDataIds(); var numOutputDataIds = 0; outInfos.forEach(function(info) { numOutputDataIds += info.dtype === "complex64" ? 3 : 1; }); var numMoves = this.state.numDataMovesStack[this.state.numDataMovesStack.length - 1]; var dataIdsLeaked = numDataIdsAfter - numDataIdsBefore - numOutputDataIds - numMoves; if (dataIdsLeaked > 0) { throw new Error("Backend '" + this.backendName + "' has an internal memory leak " + ("(" + dataIdsLeaked + " data ids) after running '" + kernelName + "'")); } }; Engine2.prototype.runKernelFunc = function(forwardFunc, inputs, backwardsFunc, kernelName, attrs, inputsToSave, outputsToSave) { var _this = this; var outputs; var saved = []; var isTapeOn = this.isTapeOn(); if (kernelName == null) { kernelName = this.state.activeScope != null ? this.state.activeScope.name : ""; } var startingBytecount = this.state.numBytes; var startingNumTensors = this.state.numTensors; if (this.shouldCheckForMemLeaks()) { this.state.numDataMovesStack.push(0); } var kernelFunc3; var kernel = getKernel(kernelName, this.backendName); var out; if (kernel != null) { kernelFunc3 = function() { var numDataIdsBefore = _this.backend.numDataIds(); out = kernel.kernelFunc({inputs, attrs, backend: _this.backend}); var outInfos = Array.isArray(out) ? out : [out]; if (_this.shouldCheckForMemLeaks()) { _this.checkKernelForMemLeak(kernelName, numDataIdsBefore, outInfos); } var outTensors = outInfos.map(function(_a) { var dataId = _a.dataId, shape = _a.shape, dtype = _a.dtype; return _this.makeTensorFromDataId(dataId, shape, dtype); }); if (isTapeOn) { var tensorsToSave = _this.getTensorsForGradient(kernelName, inputs, outTensors); if (tensorsToSave == null) { if (outputsToSave == null) { outputsToSave = []; } var outsToSave = outTensors.filter(function(_, i) { return outputsToSave[i]; }); tensorsToSave = (inputsToSave || []).slice().concat(outsToSave); } saved = _this.saveTensorsForBackwardMode(tensorsToSave); } return outTensors; }; } else { var saveFunc_1 = function(tensors) { if (!isTapeOn) { return; } saved = tensors.map(function(tensor2) { return _this.keep(_this.clone(tensor2)); }); }; kernelFunc3 = function() { var numDataIdsBefore = _this.backend.numDataIds(); out = _this.tidy(function() { return forwardFunc(_this.backend, saveFunc_1); }); var outs = Array.isArray(out) ? out : [out]; if (_this.shouldCheckForMemLeaks()) { _this.checkKernelForMemLeak(kernelName, numDataIdsBefore, outs); } return outs; }; } var kernelProfile; this.scopedRun(function() { return _this.state.kernelDepth++; }, function() { return _this.state.kernelDepth--; }, function() { if (!_this.ENV.getBool("DEBUG") && !_this.state.profiling) { outputs = kernelFunc3(); } else { kernelProfile = _this.profiler.profileKernel(kernelName, inputs, function() { return kernelFunc3(); }); if (_this.ENV.getBool("DEBUG")) { _this.profiler.logKernelProfile(kernelProfile); } outputs = kernelProfile.outputs; } }); if (isTapeOn) { this.addTapeNode(kernelName, inputs, outputs, backwardsFunc, saved, attrs); } if (this.state.profiling) { this.state.activeProfile.kernels.push({ name: kernelName, bytesAdded: this.state.numBytes - startingBytecount, totalBytesSnapshot: this.state.numBytes, tensorsAdded: this.state.numTensors - startingNumTensors, totalTensorsSnapshot: this.state.numTensors, inputShapes: Object.keys(inputs).map(function(key) { return inputs[key] != null ? inputs[key].shape : null; }), outputShapes: outputs.map(function(item) { return item.shape; }), kernelTimeMs: kernelProfile.timeMs, extraInfo: kernelProfile.extraInfo }); } return Array.isArray(out) ? outputs : outputs[0]; }; Engine2.prototype.saveTensorsForBackwardMode = function(tensors) { var _this = this; var saved = tensors.map(function(tensor2) { return _this.keep(_this.clone(tensor2)); }); return saved; }; Engine2.prototype.getTensorsForGradient = function(kernelName, inputs, outputs) { var gradConfig = getGradient(kernelName); if (gradConfig != null) { var inputsToSave = gradConfig.inputsToSave || []; var outputsToSave_1 = gradConfig.outputsToSave || []; var inputTensorsToSave = void 0; if (gradConfig.saveAllInputs) { assert(Array.isArray(inputs), function() { return "saveAllInputs is true, expected inputs to be an array."; }); inputTensorsToSave = Object.keys(inputs).map(function(key) { return inputs[key]; }); } else { inputTensorsToSave = inputsToSave.map(function(inputName) { return inputs[inputName]; }); } var outputTensorsToSave = outputs.filter(function(_, i) { return outputsToSave_1[i]; }); return inputTensorsToSave.concat(outputTensorsToSave); } return null; }; Engine2.prototype.makeTensor = function(values, shape, dtype, backend2) { if (values == null) { throw new Error("Values passed to engine.makeTensor() are null"); } dtype = dtype || "float32"; backend2 = backend2 || this.backend; var backendVals = values; if (dtype === "string" && isString(values[0])) { backendVals = values.map(function(d) { return encodeString(d); }); } var dataId = backend2.write(backendVals, shape, dtype); var t = new Tensor(shape, dtype, dataId, this.nextTensorId()); this.incRef(t, backend2); if (dtype === "string") { var info = this.state.tensorInfo.get(dataId); var newBytes = bytesFromStringArray(backendVals); this.state.numBytes += newBytes - info.bytes; info.bytes = newBytes; } return t; }; Engine2.prototype.makeTensorFromDataId = function(dataId, shape, dtype, backend2) { dtype = dtype || "float32"; var t = new Tensor(shape, dtype, dataId, this.nextTensorId()); this.incRef(t, backend2); return t; }; Engine2.prototype.makeVariable = function(initialValue, trainable, name, dtype) { if (trainable === void 0) { trainable = true; } name = name || this.nextVariableId().toString(); if (dtype != null && dtype !== initialValue.dtype) { initialValue = initialValue.cast(dtype); } var v = new Variable(initialValue, trainable, name, this.nextTensorId()); if (this.state.registeredVariables[v.name] != null) { throw new Error("Variable with name " + v.name + " was already registered"); } this.state.registeredVariables[v.name] = v; this.incRef(v, this.backend); return v; }; Engine2.prototype.incRef = function(a, backend2) { var refCount = this.state.tensorInfo.has(a.dataId) ? this.state.tensorInfo.get(a.dataId).refCount : 0; this.state.numTensors++; if (a.dtype === "string") { this.state.numStringTensors++; } if (refCount === 0) { this.state.numDataBuffers++; var bytes = 0; if (a.dtype !== "complex64" && a.dtype !== "string") { bytes = a.size * bytesPerElement(a.dtype); } this.state.tensorInfo.set(a.dataId, { backend: backend2 || this.backend, dtype: a.dtype, shape: a.shape, bytes, refCount: 0 }); this.state.numBytes += bytes; } this.state.tensorInfo.get(a.dataId).refCount++; if (!(a instanceof Variable)) { this.track(a); } }; Engine2.prototype.disposeTensor = function(a) { if (!this.state.tensorInfo.has(a.dataId)) { return; } this.state.numTensors--; if (a.dtype === "string") { this.state.numStringTensors--; } var info = this.state.tensorInfo.get(a.dataId); var refCount = info.refCount; if (refCount <= 1) { if (a.dtype !== "complex64") { this.state.numBytes -= info.bytes; } this.state.numDataBuffers--; info.backend.disposeData(a.dataId); this.state.tensorInfo.delete(a.dataId); } else { this.state.tensorInfo.get(a.dataId).refCount--; } }; Engine2.prototype.disposeVariables = function() { for (var varName in this.state.registeredVariables) { var v = this.state.registeredVariables[varName]; this.disposeVariable(v); } }; Engine2.prototype.disposeVariable = function(v) { this.disposeTensor(v); if (this.state.registeredVariables[v.name] != null) { delete this.state.registeredVariables[v.name]; } }; Engine2.prototype.memory = function() { var info = this.backend.memory(); info.numTensors = this.state.numTensors; info.numDataBuffers = this.state.numDataBuffers; info.numBytes = this.state.numBytes; if (this.state.numStringTensors > 0) { info.unreliable = true; if (info.reasons == null) { info.reasons = []; } info.reasons.push("Memory usage by string tensors is approximate (2 bytes per character)"); } return info; }; Engine2.prototype.profile = function(query) { return __awaiter(this, void 0, void 0, function() { var startBytes, startNumTensors, _a, _i2, _b, kernel, _c, _d; return __generator(this, function(_e) { switch (_e.label) { case 0: this.state.profiling = true; startBytes = this.state.numBytes; startNumTensors = this.state.numTensors; this.state.activeProfile.kernels = []; _a = this.state.activeProfile; return [4, query()]; case 1: _a.result = _e.sent(); this.state.profiling = false; this.state.activeProfile.peakBytes = Math.max.apply(Math, this.state.activeProfile.kernels.map(function(d) { return d.totalBytesSnapshot; })); this.state.activeProfile.newBytes = this.state.numBytes - startBytes; this.state.activeProfile.newTensors = this.state.numTensors - startNumTensors; _i2 = 0, _b = this.state.activeProfile.kernels; _e.label = 2; case 2: if (!(_i2 < _b.length)) return [3, 6]; kernel = _b[_i2]; _c = kernel; return [4, kernel.kernelTimeMs]; case 3: _c.kernelTimeMs = _e.sent(); _d = kernel; return [4, kernel.extraInfo]; case 4: _d.extraInfo = _e.sent(); _e.label = 5; case 5: _i2++; return [3, 2]; case 6: return [2, this.state.activeProfile]; } }); }); }; Engine2.prototype.isTapeOn = function() { return this.state.gradientDepth > 0 && this.state.kernelDepth === 0; }; Engine2.prototype.addTapeNode = function(kernelName, inputs, outputs, gradientsFunc, saved, attrs) { var _this = this; var tapeNode = {id: this.state.nextTapeNodeId++, kernelName, inputs, outputs, saved}; var gradConfig = getGradient(kernelName); if (gradConfig != null) { gradientsFunc = gradConfig.gradFunc; } if (gradientsFunc != null) { tapeNode.gradient = function(dys) { dys = dys.map(function(dy, i) { if (dy == null) { var output = outputs[i]; var vals = makeZerosTypedArray(output.size, output.dtype); return _this.makeTensor(vals, output.shape, output.dtype); } return dy; }); return gradientsFunc(dys.length > 1 ? dys : dys[0], saved, attrs); }; } this.state.activeTape.push(tapeNode); }; Engine2.prototype.keep = function(result) { result.kept = true; return result; }; Engine2.prototype.startTape = function() { if (this.state.gradientDepth === 0) { this.state.activeTape = []; } this.state.gradientDepth++; }; Engine2.prototype.endTape = function() { this.state.gradientDepth--; }; Engine2.prototype.startScope = function(name) { var scopeInfo = { track: [], name: "unnamed scope", id: this.state.nextScopeId++ }; if (name) { scopeInfo.name = name; } this.state.scopeStack.push(scopeInfo); this.state.activeScope = scopeInfo; }; Engine2.prototype.endScope = function(result) { var _this = this; var tensorsToTrackInParent = getTensorsInContainer(result); var tensorsToTrackInParentSet = new Set(tensorsToTrackInParent.map(function(t) { return t.id; })); for (var i = 0; i < this.state.activeScope.track.length; i++) { var tensor2 = this.state.activeScope.track[i]; if (!tensor2.kept && !tensorsToTrackInParentSet.has(tensor2.id)) { tensor2.dispose(); } } var oldScope = this.state.scopeStack.pop(); this.state.activeScope = this.state.scopeStack.length === 0 ? null : this.state.scopeStack[this.state.scopeStack.length - 1]; tensorsToTrackInParent.forEach(function(tensor3) { if (!tensor3.kept && tensor3.scopeId === oldScope.id) { _this.track(tensor3); } }); }; Engine2.prototype.gradients = function(f, xs, dy, allowNoGradients) { var _this = this; if (allowNoGradients === void 0) { allowNoGradients = false; } assert(xs.length > 0, function() { return "gradients() received an empty list of xs."; }); if (dy != null && dy.dtype !== "float32") { throw new Error("dy must have 'float32' dtype, but has '" + dy.dtype + "'"); } var y = this.scopedRun(function() { return _this.startTape(); }, function() { return _this.endTape(); }, function() { return _this.tidy("forward", f); }); assert(y instanceof Tensor, function() { return "The result y returned by f() must be a tensor."; }); var filteredTape = getFilteredNodesXToY(this.state.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."); } return this.tidy("backward", function() { var accumulatedGradientMap = {}; accumulatedGradientMap[y.id] = dy == null ? ones(y.shape) : dy; backpropagateGradients(accumulatedGradientMap, filteredTape, function(f2) { return _this.tidy(f2); }, add); var grads2 = xs.map(function(x) { return accumulatedGradientMap[x.id]; }); if (_this.state.gradientDepth === 0) { _this.state.activeTape.forEach(function(node) { for (var _i2 = 0, _a = node.saved; _i2 < _a.length; _i2++) { var tensor2 = _a[_i2]; tensor2.dispose(); } }); _this.state.activeTape = null; } return {value: y, grads: grads2}; }); }; Engine2.prototype.customGrad = function(f) { var _this = this; assert(isFunction(f), function() { return "The f passed in customGrad(f) must be a function."; }); return function() { var inputs = []; for (var _i2 = 0; _i2 < arguments.length; _i2++) { inputs[_i2] = arguments[_i2]; } assert(inputs.every(function(t) { return t instanceof Tensor; }), function() { return "The args passed in customGrad(f)(x1, x2,...) must all be tensors"; }); var res; var inputMap = {}; inputs.forEach(function(input, i) { inputMap[i] = input; }); return _this.runKernelFunc(function(_, save) { res = f.apply(void 0, inputs.concat([save])); assert(res.value instanceof Tensor, function() { return "The function f passed in customGrad(f) must return an object where `obj.value` is a tensor"; }); assert(isFunction(res.gradFunc), function() { return "The function f passed in customGrad(f) must return an object where `obj.gradFunc` is a function."; }); return res.value; }, inputMap, function(dy, saved) { var gradRes = res.gradFunc(dy, saved); var grads2 = Array.isArray(gradRes) ? gradRes : [gradRes]; assert(grads2.length === inputs.length, function() { return "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(grads2.every(function(t) { return t instanceof Tensor; }), function() { return "The function f passed in customGrad(f) must return an object where `obj.gradFunc` is a function that returns a list of only tensors."; }); var gradMap = {}; grads2.forEach(function(grad2, i) { gradMap[i] = function() { return grad2; }; }); return gradMap; }); }; }; Engine2.prototype.readSync = function(dataId) { var info = this.state.tensorInfo.get(dataId); return info.backend.readSync(dataId); }; Engine2.prototype.read = function(dataId) { var info = this.state.tensorInfo.get(dataId); return info.backend.read(dataId); }; Engine2.prototype.time = function(query) { return __awaiter(this, void 0, void 0, function() { var start, timingInfo; return __generator(this, function(_a) { switch (_a.label) { case 0: start = now2(); return [4, this.backend.time(query)]; case 1: timingInfo = _a.sent(); timingInfo.wallMs = now2() - start; return [2, timingInfo]; } }); }); }; Engine2.prototype.track = function(result) { if (this.state.activeScope != null) { result.scopeId = this.state.activeScope.id; this.state.activeScope.track.push(result); } return result; }; Object.defineProperty(Engine2.prototype, "registeredVariables", { get: function() { return this.state.registeredVariables; }, enumerable: true, configurable: true }); Engine2.prototype.reset = function() { this.pendingBackendInitId++; this.state.dispose(); this.ENV.reset(); this.state = new EngineState(); for (var backendName in this.registry) { this.disposeRegisteredKernels(backendName); this.registry[backendName].dispose(); delete this.registry[backendName]; } this.backendName = null; this.backendInstance = null; this.pendingBackendInit = null; }; Engine2.nextTensorId = 0; Engine2.nextVariableId = 0; return Engine2; }(); function ones(shape) { var values = makeOnesTypedArray(sizeFromShape(shape), "float32"); return ENGINE.makeTensor(values, shape, "float32"); } function getOrMakeEngine() { var ns = getGlobalNamespace(); if (ns._tfengine == null) { var environment = new Environment(ns); ns._tfengine = new Engine(environment); } setEnvironmentGlobal(ns._tfengine.ENV); setTensorTracker(function() { return ns._tfengine; }); return ns._tfengine; } var ENGINE = getOrMakeEngine(); function add(a, b) { var inputs = {a, b}; return ENGINE.runKernelFunc(function(backend2, save) { var res = backend2.add(a, b); save([a, b]); return res; }, inputs, null, Add3); } /** * @license * Copyright 2017 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function _isNavigatorDefined() { return typeof navigator !== "undefined" && navigator != null; } function isMobile() { if (_isNavigatorDefined()) { 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)); } return false; } function isBrowser() { return typeof window !== "undefined" && window.document != null || typeof WorkerGlobalScope !== "undefined"; } var device_util = { __proto__: null, isMobile, isBrowser }; /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ var ENV2 = env3(); ENV2.registerFlag("DEBUG", function() { return false; }, function(debugValue) { if (debugValue) { 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."); } }); ENV2.registerFlag("IS_BROWSER", function() { return isBrowser(); }); ENV2.registerFlag("IS_NODE", function() { return typeof process !== "undefined" && typeof process.versions !== "undefined" && typeof process.versions.node !== "undefined"; }); ENV2.registerFlag("IS_CHROME", function() { return typeof navigator !== "undefined" && navigator != null && navigator.userAgent != null && /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); }); ENV2.registerFlag("PROD", function() { return false; }); ENV2.registerFlag("TENSORLIKE_CHECK_SHAPE_CONSISTENCY", function() { return ENV2.getBool("DEBUG"); }); ENV2.registerFlag("DEPRECATION_WARNINGS_ENABLED", function() { return true; }); ENV2.registerFlag("IS_TEST", function() { return false; }); /** * @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. * ============================================================================= */ function inferShape(val, dtype) { var firstElem = val; if (isTypedArray(val)) { return dtype === "string" ? [] : [val.length]; } if (!Array.isArray(val)) { return []; } var shape = []; while (Array.isArray(firstElem) || isTypedArray(firstElem) && dtype !== "string") { shape.push(firstElem.length); firstElem = firstElem[0]; } if (Array.isArray(val) && env3().getBool("TENSORLIKE_CHECK_SHAPE_CONSISTENCY")) { deepAssertShapeConsistency(val, shape, []); } return shape; } function deepAssertShapeConsistency(val, shape, indices) { indices = indices || []; if (!Array.isArray(val) && !isTypedArray(val)) { assert(shape.length === 0, function() { return "Element arr[" + indices.join("][") + "] is a primitive, " + ("but should be an array/TypedArray of " + shape[0] + " elements"); }); return; } assert(shape.length > 0, function() { return "Element arr[" + indices.join("][") + "] should be a primitive, " + ("but is an array of " + val.length + " elements"); }); assert(val.length === shape[0], function() { return "Element arr[" + indices.join("][") + "] should have " + shape[0] + " " + ("elements, but has " + val.length + " elements"); }); var subShape = shape.slice(1); for (var i = 0; i < val.length; ++i) { deepAssertShapeConsistency(val[i], subShape, indices.concat(i)); } } function assertDtype(expectedDtype, actualDType, argName, functionName) { if (expectedDtype == null) { return; } if (expectedDtype !== "numeric" && expectedDtype !== actualDType || expectedDtype === "numeric" && actualDType === "string") { throw new Error("Argument '" + argName + "' passed to '" + functionName + "' must " + ("be " + expectedDtype + " tensor, but got " + actualDType + " tensor")); } } function convertToTensor(x, argName, functionName, parseAsDtype) { if (parseAsDtype === void 0) { parseAsDtype = "numeric"; } if (x instanceof Tensor) { assertDtype(parseAsDtype, x.dtype, argName, functionName); return x; } var inferredDtype = inferDtype(x); if (inferredDtype !== "string" && ["bool", "int32", "float32"].indexOf(parseAsDtype) >= 0) { inferredDtype = parseAsDtype; } assertDtype(parseAsDtype, inferredDtype, argName, functionName); if (x == null || !isTypedArray(x) && !Array.isArray(x) && typeof x !== "number" && typeof x !== "boolean" && typeof x !== "string") { var type = x == null ? "null" : x.constructor.name; throw new Error("Argument '" + argName + "' passed to '" + functionName + "' must be a " + ("Tensor or TensorLike, but got '" + type + "'")); } var inferredShape = inferShape(x, inferredDtype); if (!isTypedArray(x) && !Array.isArray(x)) { x = [x]; } var skipTypedArray = true; var values = inferredDtype !== "string" ? toTypedArray(x, inferredDtype) : flatten(x, [], skipTypedArray); return ENGINE.makeTensor(values, inferredShape, inferredDtype); } function convertToTensorArray(arg, argName, functionName, parseAsDtype) { if (parseAsDtype === void 0) { parseAsDtype = "numeric"; } if (!Array.isArray(arg)) { throw new Error("Argument " + argName + " passed to " + functionName + " must be a `Tensor[]` or `TensorLike[]`"); } var tensors = arg; return tensors.map(function(t, i) { return convertToTensor(t, argName + "[" + i + "]", functionName); }, parseAsDtype); } /** * @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 OP_SCOPE_SUFFIX = "__op"; function op(f) { var keys = Object.keys(f); if (keys.length !== 1) { throw new Error("Please provide an object with a single key (operation name) mapping to a function. Got an object with " + (keys.length + " keys.")); } var opName = keys[0]; var fn = f[opName]; if (opName.endsWith("_")) { opName = opName.substring(0, opName.length - 1); } opName = opName + OP_SCOPE_SUFFIX; var f2 = function() { var args = []; for (var _i2 = 0; _i2 < arguments.length; _i2++) { args[_i2] = arguments[_i2]; } ENGINE.startScope(opName); try { var result = fn.apply(void 0, args); if (isPromise(result)) { console.error("Cannot return a Promise inside of tidy."); } ENGINE.endScope(result); return result; } catch (ex) { ENGINE.endScope(null); throw ex; } }; Object.defineProperty(f2, "name", {value: opName, configurable: true}); return f2; } /** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function complex_(real2, imag2) { var $real = convertToTensor(real2, "real", "complex"); var $imag = convertToTensor(imag2, "imag", "complex"); assertShapesMatch($real.shape, $imag.shape, "real and imag shapes, " + $real.shape + " and " + $imag.shape + ", must match in call to tf.complex()."); var forward = function(backend2) { return backend2.complex($real, $imag); }; var inputs = {real: $real, imag: $imag}; return ENGINE.runKernelFunc(forward, inputs, null, Complex); } var complex = op({complex_}); /** * @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. * ============================================================================= */ function makeTensor(values, shape, inferredShape, dtype) { if (dtype == null) { dtype = inferDtype(values); } if (dtype === "complex64") { throw new Error("Cannot construct a complex64 tensor directly. Please use tf.complex(real, imag)."); } if (!isTypedArray(values) && !Array.isArray(values) && typeof values !== "number" && typeof values !== "boolean" && typeof values !== "string") { throw new Error("values passed to tensor(values) must be a number/boolean/string or an array of numbers/booleans/strings, or a TypedArray"); } if (shape != null) { assertNonNegativeIntegerDimensions(shape); var providedSize_1 = sizeFromShape(shape); var inferredSize_1 = sizeFromShape(inferredShape); assert(providedSize_1 === inferredSize_1, function() { return "Based on the provided shape, [" + shape + "], the tensor should have " + (providedSize_1 + " values but has " + inferredSize_1); }); for (var i = 0; i < inferredShape.length; ++i) { var inferred = inferredShape[i]; var flatDimsDontMatch = i === inferredShape.length - 1 ? inferred !== sizeFromShape(shape.slice(i)) : true; assert(inferredShape[i] === shape[i] || !flatDimsDontMatch, function() { return "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; values = dtype !== "string" ? toTypedArray(values, dtype) : flatten(values, [], true); return ENGINE.makeTensor(values, shape, dtype); } /** * @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. * ============================================================================= */ function tensor(values, shape, dtype) { var inferredShape = inferShape(values, dtype); return makeTensor(values, shape, inferredShape, dtype); } /** * @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 DTYPE_VALUE_SIZE_MAP = { float32: 4, float16: 2, int32: 4, uint16: 2, uint8: 1, bool: 1, complex64: 8 }; /** * @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 NUM_BYTES_STRING_LENGTH = 4; function encodeWeights(tensors, group) { return __awaiter(this, void 0, void 0, function() { var specs, dataPromises, names, _loop_1, i, tensorValues; var _this = this; return __generator(this, function(_a) { switch (_a.label) { case 0: specs = []; dataPromises = []; names = Array.isArray(tensors) ? tensors.map(function(tensor2) { return tensor2.name; }) : Object.keys(tensors); _loop_1 = function(i2) { var name_1 = names[i2]; var t = Array.isArray(tensors) ? tensors[i2].tensor : tensors[name_1]; if (t.dtype !== "float32" && t.dtype !== "int32" && t.dtype !== "bool" && t.dtype !== "string" && t.dtype !== "complex64") { throw new Error("Unsupported dtype in weight '" + name_1 + "': " + t.dtype); } var spec = {name: name_1, shape: t.shape, dtype: t.dtype}; if (t.dtype === "string") { var utf8bytes = new Promise(function(resolve) { return __awaiter(_this, void 0, void 0, function() { var vals, totalNumBytes, bytes, offset, i_1, val, bytesOfLength; return __generator(this, function(_a2) { switch (_a2.label) { case 0: return [4, t.bytes()]; case 1: vals = _a2.sent(); totalNumBytes = vals.reduce(function(p, c) { return p + c.length; }, 0) + NUM_BYTES_STRING_LENGTH * vals.length; bytes = new Uint8Array(totalNumBytes); offset = 0; for (i_1 = 0; i_1 < vals.length; i_1++) { val = vals[i_1]; bytesOfLength = new Uint8Array(new Uint32Array([val.length]).buffer); bytes.set(bytesOfLength, offset); offset += NUM_BYTES_STRING_LENGTH; bytes.set(val, offset); offset += val.length; } resolve(bytes); return [2]; } }); }); }); dataPromises.push(utf8bytes); } else { dataPromises.push(t.data()); } if (group != null) { spec.group = group; } specs.push(spec); }; for (i = 0; i < names.length; ++i) { _loop_1(i); } return [4, Promise.all(dataPromises)]; case 1: tensorValues = _a.sent(); return [2, {data: concatenateTypedArrays(tensorValues), specs}]; } }); }); } function decodeWeights(buffer3, specs) { var out = {}; var float16Decode; var offset = 0; for (var _i2 = 0, specs_1 = specs; _i2 < specs_1.length; _i2++) { var spec = specs_1[_i2]; var name_2 = spec.name; var dtype = spec.dtype; var shape = spec.shape; var size = sizeFromShape(shape); var values = void 0; if ("quantization" in spec) { var quantization = spec.quantization; if (quantization.dtype === "uint8" || quantization.dtype === "uint16") { if (!("min" in quantization && "scale" in quantization)) { throw new Error("Weight " + spec.name + " with quantization " + quantization.dtype + " doesn't have corresponding metadata min and scale."); } } else if (quantization.dtype === "float16") { if (dtype !== "float32") { throw new Error("Weight " + spec.name + " is quantized with " + quantization.dtype + " " + ("which only supports weights of type float32 not " + dtype + ".")); } } else { throw new Error("Weight " + spec.name + " has unknown " + ("quantization dtype " + quantization.dtype + ". ") + "Supported quantization dtypes are: 'uint8', 'uint16', and 'float16'."); } var quantizationSizeFactor = DTYPE_VALUE_SIZE_MAP[quantization.dtype]; var byteBuffer = buffer3.slice(offset, offset + size * quantizationSizeFactor); var quantizedArray = quantization.dtype === "uint8" ? new Uint8Array(byteBuffer) : new Uint16Array(byteBuffer); if (dtype === "float32") { if (quantization.dtype === "uint8" || quantization.dtype === "uint16") { values = new Float32Array(quantizedArray.length); for (var i = 0; i < quantizedArray.length; i++) { var v = quantizedArray[i]; values[i] = v * quantization.scale + quantization.min; } } else if (quantization.dtype === "float16") { if (float16Decode === void 0) { float16Decode = getFloat16Decoder(); } values = float16Decode(quantizedArray); } else { throw new Error("Unsupported quantization type " + quantization.dtype + " for weight type float32."); } } else if (dtype === "int32") { if (quantization.dtype !== "uint8" && quantization.dtype !== "uint16") { throw new Error("Unsupported quantization type " + quantization.dtype + " for weight type int32."); } values = new Int32Array(quantizedArray.length); for (var i = 0; i < quantizedArray.length; i++) { var v = quantizedArray[i]; values[i] = Math.round(v * quantization.scale + quantization.min); } } else { throw new Error("Unsupported dtype in weight '" + name_2 + "': " + dtype); } offset += size * quantizationSizeFactor; } else if (dtype === "string") { var size_1 = sizeFromShape(spec.shape); values = []; for (var i = 0; i < size_1; i++) { var byteLength = new Uint32Array(buffer3.slice(offset, offset + NUM_BYTES_STRING_LENGTH))[0]; offset += NUM_BYTES_STRING_LENGTH; var bytes = new Uint8Array(buffer3.slice(offset, offset + byteLength)); values.push(bytes); offset += byteLength; } } else { var dtypeFactor = DTYPE_VALUE_SIZE_MAP[dtype]; var byteBuffer = buffer3.slice(offset, offset + size * dtypeFactor); if (dtype === "float32") { values = new Float32Array(byteBuffer); } else if (dtype === "int32") { values = new Int32Array(byteBuffer); } else if (dtype === "bool") { values = new Uint8Array(byteBuffer); } else if (dtype === "complex64") { values = new Float32Array(byteBuffer); var real2 = new Float32Array(values.length / 2); var image3 = new Float32Array(values.length / 2); for (var i = 0; i < real2.length; i++) { real2[i] = values[i * 2]; image3[i] = values[i * 2 + 1]; } var realTensor = tensor(real2, shape, "float32"); var imageTensor = tensor(image3, shape, "float32"); out[name_2] = complex(realTensor, imageTensor); realTensor.dispose(); imageTensor.dispose(); } else { throw new Error("Unsupported dtype in weight '" + name_2 + "': " + dtype); } offset += size * dtypeFactor; } if (dtype !== "complex64") { out[name_2] = tensor(values, shape, dtype); } } return out; } function concatenateTypedArrays(xs) { if (xs === null) { throw new Error("Invalid input value: " + JSON.stringify(xs)); } var totalByteLength = 0; var normalizedXs = []; xs.forEach(function(x) { totalByteLength += x.byteLength; normalizedXs.push(x.byteLength === x.buffer.byteLength ? x : new x.constructor(x)); if (!(x instanceof Float32Array || x instanceof Int32Array || x instanceof Uint8Array)) { throw new Error("Unsupported TypedArray subtype: " + x.constructor.name); } }); var y = new Uint8Array(totalByteLength); var offset = 0; normalizedXs.forEach(function(x) { y.set(new Uint8Array(x.buffer), offset); offset += x.byteLength; }); return y.buffer; } var useNodeBuffer = typeof Buffer !== "undefined" && (typeof Blob === "undefined" || typeof atob === "undefined" || typeof btoa === "undefined"); function stringByteLength(str) { if (useNodeBuffer) { return Buffer.byteLength(str); } return new Blob([str]).size; } function arrayBufferToBase64String(buffer3) { if (useNodeBuffer) { return Buffer.from(buffer3).toString("base64"); } var buf = new Uint8Array(buffer3); var s = ""; for (var i = 0, l = buf.length; i < l; i++) { s += String.fromCharCode(buf[i]); } return btoa(s); } function base64StringToArrayBuffer(str) { if (useNodeBuffer) { var buf = Buffer.from(str, "base64"); return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); } var s = atob(str); var buffer3 = new Uint8Array(s.length); for (var i = 0; i < s.length; ++i) { buffer3.set([s.charCodeAt(i)], i); } return buffer3.buffer; } function concatenateArrayBuffers(buffers) { if (buffers.length === 1) { return buffers[0]; } var totalByteLength = 0; buffers.forEach(function(buffer3) { totalByteLength += buffer3.byteLength; }); var temp = new Uint8Array(totalByteLength); var offset = 0; buffers.forEach(function(buffer3) { temp.set(new Uint8Array(buffer3), offset); offset += buffer3.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 }; } function computeFloat16MantisaTable() { var convertMantissa = function(i2) { var m = i2 << 13; var e = 0; while ((m & 8388608) === 0) { e -= 8388608; m <<= 1; } m &= ~8388608; e += 947912704; return m | e; }; var mantisaTable = new Uint32Array(2048); mantisaTable[0] = 0; for (var i = 1; i < 1024; i++) { mantisaTable[i] = convertMantissa(i); } for (var i = 1024; i < 2048; i++) { mantisaTable[i] = 939524096 + (i - 1024 << 13); } return mantisaTable; } function computeFloat16ExponentTable() { var exponentTable = new Uint32Array(64); exponentTable[0] = 0; exponentTable[31] = 1199570944; exponentTable[32] = 2147483648; exponentTable[63] = 3347054592; for (var i = 1; i < 31; i++) { exponentTable[i] = i << 23; } for (var i = 33; i < 63; i++) { exponentTable[i] = 2147483648 + (i - 32 << 23); } return exponentTable; } function computeFloat16OffsetTable() { var offsetTable = new Uint32Array(64); for (var i = 0; i < 64; i++) { offsetTable[i] = 1024; } offsetTable[0] = offsetTable[32] = 0; return offsetTable; } function getFloat16Decoder() { var mantisaTable = computeFloat16MantisaTable(); var exponentTable = computeFloat16ExponentTable(); var offsetTable = computeFloat16OffsetTable(); return function(quantizedArray) { var buffer3 = new ArrayBuffer(4 * quantizedArray.length); var bufferUint32View = new Uint32Array(buffer3); for (var index = 0; index < quantizedArray.length; index++) { var float16Bits = quantizedArray[index]; var float32Bits = mantisaTable[offsetTable[float16Bits >> 10] + (float16Bits & 1023)] + exponentTable[float16Bits >> 10]; bufferUint32View[index] = float32Bits; } return new Float32Array(buffer3); }; } /** * @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 IORouterRegistry = function() { function IORouterRegistry2() { this.saveRouters = []; this.loadRouters = []; } IORouterRegistry2.getInstance = function() { if (IORouterRegistry2.instance == null) { IORouterRegistry2.instance = new IORouterRegistry2(); } return IORouterRegistry2.instance; }; IORouterRegistry2.registerSaveRouter = function(saveRouter) { IORouterRegistry2.getInstance().saveRouters.push(saveRouter); }; IORouterRegistry2.registerLoadRouter = function(loadRouter) { IORouterRegistry2.getInstance().loadRouters.push(loadRouter); }; IORouterRegistry2.getSaveHandlers = function(url) { return IORouterRegistry2.getHandlers(url, "save"); }; IORouterRegistry2.getLoadHandlers = function(url, loadOptions) { return IORouterRegistry2.getHandlers(url, "load", loadOptions); }; IORouterRegistry2.getHandlers = function(url, handlerType, loadOptions) { var validHandlers = []; var routers = handlerType === "load" ? IORouterRegistry2.getInstance().loadRouters : IORouterRegistry2.getInstance().saveRouters; routers.forEach(function(router) { var handler = router(url, loadOptions); if (handler !== null) { validHandlers.push(handler); } }); return validHandlers; }; return IORouterRegistry2; }(); var registerSaveRouter = function(loudRouter) { return IORouterRegistry.registerSaveRouter(loudRouter); }; var registerLoadRouter = function(loudRouter) { return IORouterRegistry.registerLoadRouter(loudRouter); }; var getSaveHandlers = function(url) { return IORouterRegistry.getSaveHandlers(url); }; var getLoadHandlers = function(url, loadOptions) { return IORouterRegistry.getLoadHandlers(url, loadOptions); }; /** * @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 DATABASE_NAME = "tensorflowjs"; var DATABASE_VERSION = 1; var MODEL_STORE_NAME = "models_store"; var INFO_STORE_NAME = "model_info_store"; function getIndexedDBFactory() { if (!env3().getBool("IS_BROWSER")) { throw new Error("Failed to obtain IndexedDB factory because the current environmentis not a web browser."); } var theWindow = typeof window === "undefined" ? self : 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 BrowserIndexedDB2(modelPath) { this.indexedDB = getIndexedDBFactory(); if (modelPath == null || !modelPath) { throw new Error("For IndexedDB, modelPath must not be null, undefined or empty."); } this.modelPath = modelPath; } BrowserIndexedDB2.prototype.save = function(modelArtifacts) { return __awaiter(this, void 0, void 0, function() { return __generator(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)]; }); }); }; BrowserIndexedDB2.prototype.load = function() { return __awaiter(this, void 0, void 0, function() { return __generator(this, function(_a) { return [2, this.databaseAction(this.modelPath)]; }); }); }; BrowserIndexedDB2.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 modelStore2 = modelTx_1.objectStore(MODEL_STORE_NAME); var putModelRequest = modelStore2.put({ modelPath: _this.modelPath, 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(error2) { 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); }; }); }; BrowserIndexedDB2.URL_SCHEME = "indexeddb://"; return BrowserIndexedDB2; }(); var indexedDBRouter = function(url) { if (!env3().getBool("IS_BROWSER")) { return null; } else { if (!Array.isArray(url) && 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 BrowserIndexedDBManager2() { this.indexedDB = getIndexedDBFactory(); } BrowserIndexedDBManager2.prototype.listModels = function() { return __awaiter(this, void 0, void 0, function() { var _this = this; return __generator(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 _i2 = 0, _a2 = getAllInfoRequest.result; _i2 < _a2.length; _i2++) { var item = _a2[_i2]; 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); }; })]; }); }); }; BrowserIndexedDBManager2.prototype.removeModel = function(path) { return __awaiter(this, void 0, void 0, function() { var _this = this; return __generator(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 BrowserIndexedDBManager2; }(); /** * @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 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"; var MODEL_METADATA_SUFFIX = "model_metadata"; 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), modelMetadata: [PATH_PREFIX, path, MODEL_METADATA_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 BrowserLocalStorage2(modelPath) { if (!env3().getBool("IS_BROWSER") || typeof window === "undefined" || 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); } BrowserLocalStorage2.prototype.save = function(modelArtifacts) { return __awaiter(this, void 0, void 0, function() { var topology, weightSpecs, modelArtifactsInfo; return __generator(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)); this.LS.setItem(this.keys.modelMetadata, JSON.stringify({ format: modelArtifacts.format, generatedBy: modelArtifacts.generatedBy, convertedBy: modelArtifacts.convertedBy, userDefinedMetadata: modelArtifacts.userDefinedMetadata })); return [2, {modelArtifactsInfo}]; } catch (err) { this.LS.removeItem(this.keys.info); this.LS.removeItem(this.keys.topology); this.LS.removeItem(this.keys.weightSpecs); this.LS.removeItem(this.keys.weightData); this.LS.removeItem(this.keys.modelMetadata); 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]; }); }); }; BrowserLocalStorage2.prototype.load = function() { return __awaiter(this, void 0, void 0, function() { var info, out, topology, weightSpecs, metadataString, metadata, weightDataBase64; return __generator(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; metadataString = this.LS.getItem(this.keys.modelMetadata); if (metadataString != null) { metadata = JSON.parse(metadataString); out.format = metadata["format"]; out.generatedBy = metadata["generatedBy"]; out.convertedBy = metadata["convertedBy"]; out.userDefinedMetadata = metadata["userDefinedMetadata"]; } 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]; }); }); }; BrowserLocalStorage2.URL_SCHEME = "localstorage://"; return BrowserLocalStorage2; }(); var localStorageRouter = function(url) { if (!env3().getBool("IS_BROWSER")) { return null; } else { if (!Array.isArray(url) && 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 BrowserLocalStorageManager2() { assert(env3().getBool("IS_BROWSER"), function() { return "Current environment is not a web browser"; }); assert(typeof window === "undefined" || typeof window.localStorage !== "undefined", function() { return "Current browser does not appear to support localStorage"; }); this.LS = window.localStorage; } BrowserLocalStorageManager2.prototype.listModels = function() { return __awaiter(this, void 0, void 0, function() { var out, prefix, suffix, i, key, modelPath; return __generator(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]; }); }); }; BrowserLocalStorageManager2.prototype.removeModel = function(path) { return __awaiter(this, void 0, void 0, function() { var keys, info; return __generator(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 BrowserLocalStorageManager2; }(); /** * @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 URL_SCHEME_SUFFIX = "://"; var ModelStoreManagerRegistry = function() { function ModelStoreManagerRegistry2() { this.managers = {}; } ModelStoreManagerRegistry2.getInstance = function() { if (ModelStoreManagerRegistry2.instance == null) { ModelStoreManagerRegistry2.instance = new ModelStoreManagerRegistry2(); } return ModelStoreManagerRegistry2.instance; }; ModelStoreManagerRegistry2.registerManager = function(scheme, manager) { assert(scheme != null, function() { return "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, function() { return "scheme must not be an empty string."; }); var registry = ModelStoreManagerRegistry2.getInstance(); assert(registry.managers[scheme] == null, function() { return "A model store manager is already registered for scheme '" + scheme + "'."; }); registry.managers[scheme] = manager; }; ModelStoreManagerRegistry2.getManager = function(scheme) { var manager = this.getInstance().managers[scheme]; if (manager == null) { throw new Error("Cannot find model manager for scheme '" + scheme + "'"); } return manager; }; ModelStoreManagerRegistry2.getSchemes = function() { return Object.keys(this.getInstance().managers); }; return ModelStoreManagerRegistry2; }(); 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 cloneModelInternal(sourceURL, destURL, deleteSource) { if (deleteSource === void 0) { deleteSource = false; } return __awaiter(this, void 0, void 0, function() { var loadHandlers, loadHandler, saveHandlers, saveHandler, sourceScheme, sourcePath, sameMedium, modelArtifacts, saveResult; return __generator(this, function(_a) { switch (_a.label) { case 0: assert(sourceURL !== destURL, function() { return "Old path and new path are the same: '" + sourceURL + "'"; }); loadHandlers = IORouterRegistry.getLoadHandlers(sourceURL); assert(loadHandlers.length > 0, function() { return "Copying failed because no load handler is found for source URL " + sourceURL + "."; }); assert(loadHandlers.length < 2, function() { return "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, function() { return "Copying failed because no save handler is found for destination " + ("URL " + destURL + "."); }); assert(saveHandlers.length < 2, function() { return "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 listModels() { return __awaiter(this, void 0, void 0, function() { var schemes, out, _i2, schemes_1, scheme, schemeOut, path, url; return __generator(this, function(_a) { switch (_a.label) { case 0: schemes = ModelStoreManagerRegistry.getSchemes(); out = {}; _i2 = 0, schemes_1 = schemes; _a.label = 1; case 1: if (!(_i2 < schemes_1.length)) return [3, 4]; scheme = schemes_1[_i2]; 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: _i2++; return [3, 1]; case 4: return [2, out]; } }); }); } function removeModel(url) { return __awaiter(this, void 0, void 0, function() { var schemeAndPath, manager; return __generator(this, function(_a) { schemeAndPath = parseURL(url); manager = ModelStoreManagerRegistry.getManager(schemeAndPath.scheme); return [2, manager.removeModel(schemeAndPath.path)]; }); }); } function copyModel(sourceURL, destURL) { return __awaiter(this, void 0, void 0, function() { var deleteSource; return __generator(this, function(_a) { deleteSource = false; return [2, cloneModelInternal(sourceURL, destURL, deleteSource)]; }); }); } function moveModel(sourceURL, destURL) { return __awaiter(this, void 0, void 0, function() { var deleteSource; return __generator(this, function(_a) { deleteSource = true; return [2, cloneModelInternal(sourceURL, destURL, deleteSource)]; }); }); } /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ var PlatformBrowser = function() { function PlatformBrowser2() { } PlatformBrowser2.prototype.fetch = function(path, init2) { return fetch(path, init2); }; PlatformBrowser2.prototype.now = function() { return performance.now(); }; PlatformBrowser2.prototype.encode = function(text, encoding) { if (encoding !== "utf-8" && encoding !== "utf8") { throw new Error("Browser's encoder only supports utf-8, but got " + encoding); } if (this.textEncoder == null) { this.textEncoder = new TextEncoder(); } return this.textEncoder.encode(text); }; PlatformBrowser2.prototype.decode = function(bytes, encoding) { return new TextDecoder(encoding).decode(bytes); }; return PlatformBrowser2; }(); if (env3().get("IS_BROWSER")) { env3().setPlatform("browser", new PlatformBrowser()); try { ModelStoreManagerRegistry.registerManager(BrowserLocalStorage.URL_SCHEME, new BrowserLocalStorageManager()); } catch (err) { } try { ModelStoreManagerRegistry.registerManager(BrowserIndexedDB.URL_SCHEME, new BrowserIndexedDBManager()); } catch (err) { } } /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ var getNodeFetch = { importFetch: function() { return require_lib(); } }; var systemFetch; var PlatformNode = function() { function PlatformNode2() { this.util = require("util"); this.textEncoder = new this.util.TextEncoder(); } PlatformNode2.prototype.fetch = function(path, requestInits) { if (env3().global.fetch != null) { return env3().global.fetch(path, requestInits); } if (systemFetch == null) { systemFetch = getNodeFetch.importFetch(); } return systemFetch(path, requestInits); }; PlatformNode2.prototype.now = function() { var time2 = process.hrtime(); return time2[0] * 1e3 + time2[1] / 1e6; }; PlatformNode2.prototype.encode = function(text, encoding) { if (encoding !== "utf-8" && encoding !== "utf8") { throw new Error("Node built-in encoder only supports utf-8, but got " + encoding); } return this.textEncoder.encode(text); }; PlatformNode2.prototype.decode = function(bytes, encoding) { if (bytes.length === 0) { return ""; } return new this.util.TextDecoder(encoding).decode(bytes); }; return PlatformNode2; }(); if (env3().get("IS_NODE")) { env3().setPlatform("node", new PlatformNode()); } /** * @license * Copyright 2020 Google Inc. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function buffer2(shape, dtype, values) { if (dtype === void 0) { dtype = "float32"; } dtype = dtype || "float32"; assertNonNegativeIntegerDimensions(shape); return new TensorBuffer(shape, dtype, values); } /** * @license * Copyright 2020 Google Inc. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function cast_(x, dtype) { var $x = convertToTensor(x, "x", "cast"); if (!isValidDtype(dtype)) { throw new Error("Failed to cast to unknown dtype " + dtype); } if (dtype === "string" && $x.dtype !== "string" || dtype !== "string" && $x.dtype === "string") { throw new Error("Only strings can be casted to strings"); } var inputs = {x: $x}; var attrs = {dtype}; return ENGINE.runKernelFunc(function(backend2) { return backend2.cast($x, dtype); }, inputs, null, Cast5, attrs); } var cast2 = op({cast_}); /** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function clone_(x) { var $x = convertToTensor(x, "x", "clone", null); var forward = function() { return ENGINE.makeTensorFromDataId($x.dataId, $x.shape, $x.dtype); }; var inputs = {x: $x}; return ENGINE.runKernelFunc(forward, inputs, null, Identity5); } var clone = op({clone_}); /** * @license * Copyright 2020 Google Inc. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function print2(x, verbose) { if (verbose === void 0) { verbose = false; } console.log(x.toString(verbose)); } /** * @license * Copyright 2020 Google Inc. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ getOrMakeEngine(); var opHandler$1 = { buffer: buffer2, cast: cast2, clone, print: print2 }; setOpHandler(opHandler$1); /** * @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 DEFAULT_FILE_NAME_PREFIX = "model"; var DEFAULT_JSON_EXTENSION_NAME = ".json"; var DEFAULT_WEIGHT_DATA_EXTENSION_NAME = ".weights.bin"; function defer(f) { return new Promise(function(resolve) { return setTimeout(resolve); }).then(f); } var BrowserDownloads = function() { function BrowserDownloads2(fileNamePrefix) { if (!env3().getBool("IS_BROWSER")) { throw new Error("browserDownloads() cannot proceed because the current environment is not a browser."); } if (fileNamePrefix.startsWith(BrowserDownloads2.URL_SCHEME)) { fileNamePrefix = fileNamePrefix.slice(BrowserDownloads2.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; } BrowserDownloads2.prototype.save = function(modelArtifacts) { return __awaiter(this, void 0, void 0, function() { var weightsURL, weightsManifest, modelTopologyAndWeightManifest, modelTopologyAndWeightManifestURL, jsonAnchor_1, weightDataAnchor_1; return __generator(this, function(_a) { switch (_a.label) { case 0: if (typeof document === "undefined") { throw new Error("Browser downloads are not supported in this environment since `document` is not present"); } weightsURL = window.URL.createObjectURL(new Blob([modelArtifacts.weightData], {type: "application/octet-stream"})); if (!(modelArtifacts.modelTopology instanceof ArrayBuffer)) return [3, 1]; throw new Error("BrowserDownloads.save() does not support saving model topology in binary formats yet."); case 1: weightsManifest = [{ paths: ["./" + this.weightDataFileName], weights: modelArtifacts.weightSpecs }]; modelTopologyAndWeightManifest = { modelTopology: modelArtifacts.modelTopology, format: modelArtifacts.format, generatedBy: modelArtifacts.generatedBy, convertedBy: modelArtifacts.convertedBy, weightsManifest }; modelTopologyAndWeightManifestURL = window.URL.createObjectURL(new Blob([JSON.stringify(modelTopologyAndWeightManifest)], {type: "application/json"})); jsonAnchor_1 = this.jsonAnchor == null ? document.createElement("a") : this.jsonAnchor; jsonAnchor_1.download = this.modelTopologyFileName; jsonAnchor_1.href = modelTopologyAndWeightManifestURL; return [4, defer(function() { return jsonAnchor_1.dispatchEvent(new MouseEvent("click")); })]; case 2: _a.sent(); if (!(modelArtifacts.weightData != null)) return [3, 4]; weightDataAnchor_1 = this.weightDataAnchor == null ? document.createElement("a") : this.weightDataAnchor; weightDataAnchor_1.download = this.weightDataFileName; weightDataAnchor_1.href = weightsURL; return [4, defer(function() { return weightDataAnchor_1.dispatchEvent(new MouseEvent("click")); })]; case 3: _a.sent(); _a.label = 4; case 4: return [2, {modelArtifactsInfo: getModelArtifactsInfoForJSON(modelArtifacts)}]; } }); }); }; BrowserDownloads2.URL_SCHEME = "downloads://"; return BrowserDownloads2; }(); var BrowserFiles = function() { function BrowserFiles2(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; } BrowserFiles2.prototype.load = function() { return __awaiter(this, void 0, void 0, function() { var jsonFile, weightFiles; var _this = this; return __generator(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}); } 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(event2) { var weightData = event2.target.result; var index = paths.indexOf(path); perFileBuffers[index] = weightData; if (perFileBuffers.indexOf(null) === -1) { resolve({ modelTopology, weightSpecs, weightData: concatenateArrayBuffers(perFileBuffers), format: modelJSON.format, generatedBy: modelJSON.generatedBy, convertedBy: modelJSON.convertedBy, userDefinedMetadata: modelJSON.userDefinedMetadata }); } }; weightFileReader.onerror = function(error) { return reject("Failed to weights data from file of path '" + path + "'."); }; weightFileReader.readAsArrayBuffer(pathToFile[path]); }); }); }; jsonReader.onerror = function(error) { return reject("Failed to read model topology and weights manifest JSON " + ("from file '" + jsonFile.name + "'. BrowserFiles supports loading ") + "Keras-style tf.Model artifacts only."); }; jsonReader.readAsText(jsonFile); })]; }); }); }; BrowserFiles2.prototype.checkManifestAndWeightFiles = function(manifest, files) { var basenames = []; var fileNames = files.map(function(file) { return basename(file.name); }); var pathToFile = {}; for (var _i2 = 0, manifest_1 = manifest; _i2 < manifest_1.length; _i2++) { var group = manifest_1[_i2]; 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 BrowserFiles2; }(); var browserDownloadsRouter = function(url) { if (!env3().getBool("IS_BROWSER")) { return null; } else { if (!Array.isArray(url) && 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); } /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function monitorPromisesProgress(promises, onProgress, startFraction, endFraction) { checkPromises(promises); startFraction = startFraction == null ? 0 : startFraction; endFraction = endFraction == null ? 1 : endFraction; checkFraction(startFraction, endFraction); var resolvedPromise = 0; var registerMonitor = function(promise) { promise.then(function(value) { var fraction = startFraction + ++resolvedPromise / promises.length * (endFraction - startFraction); onProgress(fraction); return value; }); return promise; }; function checkPromises(promises2) { assert(promises2 != null && Array.isArray(promises2) && promises2.length > 0, function() { return "promises must be a none empty array"; }); } function checkFraction(startFraction2, endFraction2) { assert(startFraction2 >= 0 && startFraction2 <= 1, function() { return "Progress fraction must be in range [0, 1], but " + ("got startFraction " + startFraction2); }); assert(endFraction2 >= 0 && endFraction2 <= 1, function() { return "Progress fraction must be in range [0, 1], but " + ("got endFraction " + endFraction2); }); assert(endFraction2 >= startFraction2, function() { return "startFraction must be no more than endFraction, but " + ("got startFraction " + startFraction2 + " and endFraction ") + ("" + endFraction2); }); } return Promise.all(promises.map(registerMonitor)); } /** * @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. * ============================================================================= */ function loadWeightsAsArrayBuffer(fetchURLs, loadOptions) { return __awaiter(this, void 0, void 0, function() { var fetchFunc, requests, fetchStartFraction, fetchEndFraction, responses, _a, bufferPromises, bufferStartFraction, bufferEndFraction, buffers, _b; return __generator(this, function(_c) { switch (_c.label) { case 0: if (loadOptions == null) { loadOptions = {}; } fetchFunc = loadOptions.fetchFunc == null ? env3().platform.fetch : loadOptions.fetchFunc; requests = fetchURLs.map(function(fetchURL) { return fetchFunc(fetchURL, loadOptions.requestInit, {isBinary: true}); }); fetchStartFraction = 0; fetchEndFraction = 0.5; if (!(loadOptions.onProgress == null)) return [3, 2]; return [4, Promise.all(requests)]; case 1: _a = _c.sent(); return [3, 4]; case 2: return [4, monitorPromisesProgress(requests, loadOptions.onProgress, fetchStartFraction, fetchEndFraction)]; case 3: _a = _c.sent(); _c.label = 4; case 4: responses = _a; bufferPromises = responses.map(function(response) { return response.arrayBuffer(); }); bufferStartFraction = 0.5; bufferEndFraction = 1; if (!(loadOptions.onProgress == null)) return [3, 6]; return [4, Promise.all(bufferPromises)]; case 5: _b = _c.sent(); return [3, 8]; case 6: return [4, monitorPromisesProgress(bufferPromises, loadOptions.onProgress, bufferStartFraction, bufferEndFraction)]; case 7: _b = _c.sent(); _c.label = 8; case 8: buffers = _b; return [2, buffers]; } }); }); } function loadWeights(manifest, filePathPrefix, weightNames, requestInit) { if (filePathPrefix === void 0) { filePathPrefix = ""; } return __awaiter(this, void 0, void 0, function() { var fetchWeights, loadWeights2; return __generator(this, function(_a) { fetchWeights = function(fetchUrls) { return loadWeightsAsArrayBuffer(fetchUrls, {requestInit}); }; loadWeights2 = weightsLoaderFactory(fetchWeights); return [2, loadWeights2(manifest, filePathPrefix, weightNames)]; }); }); } function weightsLoaderFactory(fetchWeightsFunction) { var _this = this; return function(manifest, filePathPrefix, weightNames) { if (filePathPrefix === void 0) { filePathPrefix = ""; } return __awaiter(_this, void 0, void 0, function() { var groupIndicesToFetchMap, groupWeightsToFetch, weightsFound, allManifestWeightNames, weightsNotFound, groupIndicesToFetch, fetchUrls, buffers, weightsTensorMap, bufferIndexOffset; return __generator(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, 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(_, 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; }, []); fetchUrls = []; groupIndicesToFetch.forEach(function(i) { manifest[i].paths.forEach(function(filepath) { var fetchUrl = filePathPrefix + (!filePathPrefix.endsWith("/") ? "/" : "") + filepath; fetchUrls.push(fetchUrl); }); }); return [4, fetchWeightsFunction(fetchUrls)]; case 1: 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 buffer3 = new Uint8Array(buffers[bufferIndexOffset + i_2]); groupByteBuffer.set(buffer3, groupBufferOffset); groupBufferOffset += buffer3.byteLength; } var weightsEntries = groupWeightsToFetch[i]; weightsEntries.forEach(function(weightsEntry) { var byteBuffer = groupBuffer.slice(weightsEntry.groupOffset, weightsEntry.groupOffset + weightsEntry.sizeBytes); var nameToTensorMap = decodeWeights(byteBuffer, [weightsEntry.manifestEntry]); for (var name_1 in nameToTensorMap) { weightsTensorMap[name_1] = nameToTensorMap[name_1]; } }); bufferIndexOffset += numBuffers; }); return [2, weightsTensorMap]; } }); }); }; } /** * @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 OCTET_STREAM_MIME_TYPE = "application/octet-stream"; var JSON_TYPE = "application/json"; var HTTPRequest = function() { function HTTPRequest2(path, loadOptions) { this.DEFAULT_METHOD = "POST"; if (loadOptions == null) { loadOptions = {}; } this.weightPathPrefix = loadOptions.weightPathPrefix; this.onProgress = loadOptions.onProgress; this.weightUrlConverter = loadOptions.weightUrlConverter; if (loadOptions.fetchFunc != null) { assert(typeof loadOptions.fetchFunc === "function", function() { return "Must pass a function that matches the signature of `fetch` (see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)"; }); this.fetch = loadOptions.fetchFunc; } else { this.fetch = env3().platform.fetch; } assert(path != null && path.length > 0, function() { return "URL path for http must not be null, undefined or empty."; }); if (Array.isArray(path)) { assert(path.length === 2, function() { return "URL paths for http must have a length of 2, " + ("(actual length is " + path.length + ")."); }); } this.path = path; if (loadOptions.requestInit != null && loadOptions.requestInit.body != null) { throw new Error("requestInit is expected to have no pre-existing body, but has one."); } this.requestInit = loadOptions.requestInit || {}; } HTTPRequest2.prototype.save = function(modelArtifacts) { return __awaiter(this, void 0, void 0, function() { var init2, weightsManifest, modelTopologyAndWeightManifest, response; return __generator(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."); } init2 = Object.assign({method: this.DEFAULT_METHOD}, this.requestInit); init2.body = new FormData(); weightsManifest = [{ paths: ["./model.weights.bin"], weights: modelArtifacts.weightSpecs }]; modelTopologyAndWeightManifest = { modelTopology: modelArtifacts.modelTopology, format: modelArtifacts.format, generatedBy: modelArtifacts.generatedBy, convertedBy: modelArtifacts.convertedBy, userDefinedMetadata: modelArtifacts.userDefinedMetadata, weightsManifest }; init2.body.append("model.json", new Blob([JSON.stringify(modelTopologyAndWeightManifest)], {type: JSON_TYPE}), "model.json"); if (modelArtifacts.weightData != null) { init2.body.append("model.weights.bin", new Blob([modelArtifacts.weightData], {type: OCTET_STREAM_MIME_TYPE}), "model.weights.bin"); } return [4, this.fetch(this.path, init2)]; case 1: response = _a.sent(); if (response.ok) { return [2, { modelArtifactsInfo: getModelArtifactsInfoForJSON(modelArtifacts), responses: [response] }]; } else { throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status " + (response.status + ".")); } } }); }); }; HTTPRequest2.prototype.load = function() { return __awaiter(this, void 0, void 0, function() { var modelConfigRequest, modelConfig, e_1, message, modelTopology, weightsManifest, generatedBy, convertedBy, format, userDefinedMetadata, weightSpecs, weightData, results, artifacts, initializer; return __generator(this, function(_a) { switch (_a.label) { case 0: return [4, this.fetch(this.path, this.requestInit)]; case 1: modelConfigRequest = _a.sent(); if (!modelConfigRequest.ok) { throw new Error("Request to " + this.path + " failed with status code " + (modelConfigRequest.status + ". Please verify this URL points to ") + "the model JSON of the model to load."); } _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); return [4, modelConfigRequest.json()]; case 3: modelConfig = _a.sent(); return [3, 5]; case 4: e_1 = _a.sent(); message = "Failed to parse model JSON of response from " + this.path + "."; if (this.path.endsWith(".pb")) { message += " Your path contains a .pb file extension. Support for .pb models have been removed in TensorFlow.js 1.0 in favor of .json models. You can re-convert your Python TensorFlow model using the TensorFlow.js 1.0 conversion scripts or you can convert your.pb models with the 'pb2json'NPM script in the tensorflow/tfjs-converter repository."; } else { message += " Please make sure the server is serving valid JSON for this request."; } throw new Error(message); case 5: modelTopology = modelConfig.modelTopology; weightsManifest = modelConfig.weightsManifest; generatedBy = modelConfig.generatedBy; convertedBy = modelConfig.convertedBy; format = modelConfig.format; userDefinedMetadata = modelConfig.userDefinedMetadata; if (modelTopology == null && weightsManifest == null) { throw new Error("The JSON from HTTP path " + this.path + " contains neither model topology or manifest for weights."); } if (!(weightsManifest != null)) return [3, 7]; return [4, this.loadWeights(weightsManifest)]; case 6: results = _a.sent(); weightSpecs = results[0], weightData = results[1]; _a.label = 7; case 7: artifacts = { modelTopology, weightSpecs, weightData, userDefinedMetadata, generatedBy, convertedBy, format }; initializer = modelConfig.modelInitializer; if (initializer) { artifacts.modelInitializer = initializer; } return [2, artifacts]; } }); }); }; HTTPRequest2.prototype.loadWeights = function(weightsManifest) { return __awaiter(this, void 0, void 0, function() { var weightPath, _a, prefix, suffix, pathPrefix, weightSpecs, _i2, weightsManifest_1, entry, fetchURLs, urlPromises, _b, weightsManifest_2, weightsGroup, _c, _d, path, _e, _f, _g, buffers; return __generator(this, function(_h) { switch (_h.label) { case 0: weightPath = Array.isArray(this.path) ? this.path[1] : this.path; _a = parseUrl(weightPath), prefix = _a[0], suffix = _a[1]; pathPrefix = this.weightPathPrefix || prefix; weightSpecs = []; for (_i2 = 0, weightsManifest_1 = weightsManifest; _i2 < weightsManifest_1.length; _i2++) { entry = weightsManifest_1[_i2]; weightSpecs.push.apply(weightSpecs, entry.weights); } fetchURLs = []; urlPromises = []; for (_b = 0, weightsManifest_2 = weightsManifest; _b < weightsManifest_2.length; _b++) { weightsGroup = weightsManifest_2[_b]; for (_c = 0, _d = weightsGroup.paths; _c < _d.length; _c++) { path = _d[_c]; if (this.weightUrlConverter != null) { urlPromises.push(this.weightUrlConverter(path)); } else { fetchURLs.push(pathPrefix + path + suffix); } } } if (!this.weightUrlConverter) return [3, 2]; _f = (_e = fetchURLs.push).apply; _g = [fetchURLs]; return [4, Promise.all(urlPromises)]; case 1: _f.apply(_e, _g.concat([_h.sent()])); _h.label = 2; case 2: return [4, loadWeightsAsArrayBuffer(fetchURLs, { requestInit: this.requestInit, fetchFunc: this.fetch, onProgress: this.onProgress })]; case 3: buffers = _h.sent(); return [2, [weightSpecs, concatenateArrayBuffers(buffers)]]; } }); }); }; HTTPRequest2.URL_SCHEME_REGEX = /^https?:\/\//; return HTTPRequest2; }(); function parseUrl(url) { var lastSlash = url.lastIndexOf("/"); var lastSearchParam = url.lastIndexOf("?"); var prefix = url.substring(0, lastSlash); var suffix = lastSearchParam > lastSlash ? url.substring(lastSearchParam) : ""; return [prefix + "/", suffix]; } function isHTTPScheme(url) { return url.match(HTTPRequest.URL_SCHEME_REGEX) != null; } var httpRouter = function(url, loadOptions) { if (typeof fetch === "undefined" && (loadOptions == null || loadOptions.fetchFunc == null)) { return null; } else { var isHTTP = true; if (Array.isArray(url)) { isHTTP = url.every(function(urlItem) { return isHTTPScheme(urlItem); }); } else { isHTTP = isHTTPScheme(url); } if (isHTTP) { return http(url, loadOptions); } } return null; }; IORouterRegistry.registerSaveRouter(httpRouter); IORouterRegistry.registerLoadRouter(httpRouter); function http(path, loadOptions) { return new HTTPRequest(path, loadOptions); } function browserHTTPRequest(path, loadOptions) { return http(path, loadOptions); } /** * @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 PassthroughLoader = function() { function PassthroughLoader2(modelArtifacts) { this.modelArtifacts = modelArtifacts; } PassthroughLoader2.prototype.load = function() { return __awaiter(this, void 0, void 0, function() { return __generator(this, function(_a) { return [2, this.modelArtifacts]; }); }); }; return PassthroughLoader2; }(); var PassthroughSaver = function() { function PassthroughSaver2(saveHandler) { this.saveHandler = saveHandler; } PassthroughSaver2.prototype.save = function(modelArtifacts) { return __awaiter(this, void 0, void 0, function() { return __generator(this, function(_a) { return [2, this.saveHandler(modelArtifacts)]; }); }); }; return PassthroughSaver2; }(); function fromMemory(modelArtifacts, weightSpecs, weightData, trainingConfig) { if (arguments.length === 1) { var isModelArtifacts = modelArtifacts.modelTopology != null || modelArtifacts.weightSpecs != null; if (isModelArtifacts) { return new PassthroughLoader(modelArtifacts); } else { console.warn("Please call tf.io.fromMemory() with only one argument. The argument should be of type ModelArtifacts. The multi-argument signature of tf.io.fromMemory() has been deprecated and will be removed in a future release."); return new PassthroughLoader({modelTopology: modelArtifacts}); } } else { console.warn("Please call tf.io.fromMemory() with only one argument. The argument should be of type ModelArtifacts. The multi-argument signature of tf.io.fromMemory() has been deprecated and will be removed in a future release."); return new PassthroughLoader({ modelTopology: modelArtifacts, weightSpecs, weightData, trainingConfig }); } } function withSaveHandler(saveHandler) { return new PassthroughSaver(saveHandler); } /** * @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 io = { __proto__: null, browserFiles, browserHTTPRequest, concatenateArrayBuffers, decodeWeights, encodeWeights, fromMemory, getLoadHandlers, getModelArtifactsInfoForJSON, getSaveHandlers, http, isHTTPScheme, loadWeights, registerLoadRouter, registerSaveRouter, weightsLoaderFactory, withSaveHandler, copyModel, listModels, moveModel, removeModel }; /** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function reshape_(x, shape) { var $x = convertToTensor(x, "x", "reshape", null); var inputs = {x: $x}; var attrs = {shape}; var forward = function(backend2, save) { shape = inferFromImplicitShape(shape, $x.size); assert($x.size === sizeFromShape(shape), function() { return "new shape and old shape must have the same number of elements."; }); save([$x]); return backend2.reshape($x, shape); }; return ENGINE.runKernelFunc(forward, inputs, null, Reshape6, attrs); } var reshape2 = op({reshape_}); /** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function matMul_(a, b, transposeA, transposeB) { var _a; if (transposeA === void 0) { transposeA = false; } if (transposeB === void 0) { transposeB = false; } var $a = convertToTensor(a, "a", "matMul"); var $b = convertToTensor(b, "b", "matMul"); _a = makeTypesMatch($a, $b), $a = _a[0], $b = _a[1]; var forward = function(backend2, save) { save([$a, $b]); var innerShapeA = transposeA ? $a.shape[$a.rank - 2] : $a.shape[$a.rank - 1]; var innerShapeB = transposeB ? $b.shape[$b.rank - 1] : $b.shape[$b.rank - 2]; var outerShapeA = transposeA ? $a.shape[$a.rank - 1] : $a.shape[$a.rank - 2]; var outerShapeB = transposeB ? $b.shape[$b.rank - 2] : $b.shape[$b.rank - 1]; var outerDimsA = $a.shape.slice(0, -2); var outerDimsB = $b.shape.slice(0, -2); var batchDimA = sizeFromShape(outerDimsA); var batchDimB = sizeFromShape(outerDimsB); var batchDimsCompatible = batchDimA === batchDimB || batchDimA === 1 || batchDimB === 1; assert($a.rank >= 2 && $b.rank >= 2 && batchDimsCompatible, function() { return "Error in matMul: the input batch dimensions must either be the same or at least one input batch dimension must be 1. Got input " + ("batch dimensions of (" + outerDimsA + ") and (" + outerDimsB + ")."); }); assert(innerShapeA === innerShapeB, function() { return "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 outShapeOuterDims = batchDimA > batchDimB ? outerDimsA : outerDimsB; var outShape = outShapeOuterDims.concat([outerShapeA, outerShapeB]); var a3D = transposeA ? reshape2($a, [batchDimA, innerShapeA, outerShapeA]) : reshape2($a, [batchDimA, outerShapeA, innerShapeA]); var b3D = transposeB ? reshape2($b, [batchDimB, outerShapeB, innerShapeB]) : reshape2($b, [batchDimB, innerShapeB, outerShapeB]); var res3d = backend2.batchMatMul(a3D, b3D, transposeA, transposeB); return reshape2(res3d, outShape); }; var inputs = {a: $a, b: $b}; var attrs = {transposeA, transposeB}; return ENGINE.runKernelFunc(forward, inputs, null, BatchMatMul3, attrs); } var matMul = op({matMul_}); /** * @license * Copyright 2020 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ function oneHot_(indices, depth, onValue, offValue) { if (onValue === void 0) { onValue = 1; } if (offValue === void 0) { offValue = 0; } if (depth < 2) { throw new Error("Error in oneHot: depth must be >=2, but it is " + depth); } var $indices = convertToTensor(indices, "indices", "oneHot", "int32"); var outShape = $indices.shape.concat([depth]); var forward = function(backend2, save) { save([$indices]); return reshape2(backend2.oneHot(reshape2($indices, [$indices.size]), depth, onValue, offValue), outShape); }; var inputs = {indices: $indices}; var attrs = {depth, onValue, offValue}; return ENGINE.runKernelFunc(forward, inputs, null, OneHot3, attrs); } var oneHot2 = op({oneHot_}); /** * @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. * ============================================================================= */ function transpose_(x, perm) { var $x = convertToTensor(x, "x", "transpose"); if (perm == null) { perm = $x.shape.map(function(s, i) { return i; }).reverse(); } assert($x.rank === perm.length, function() { return "Error in transpose: rank of input " + $x.rank + " " + ("must match length of perm " + perm + "."); }); perm.forEach(function(axis) { assert(axis >= 0 && axis < $x.rank, function() { return "All entries in 'perm' must be between 0 and " + ($x.rank - 1) + (" but got " + perm); }); }); if ($x.rank <= 1) { return $x.clone(); } var inputs = {x: $x}; var attrs = {perm}; return ENGINE.runKernelFunc(function(backend2) { return backend2.transpose($x, perm); }, inputs, null, Transpose5, attrs); } var transpose2 = op({transpose_}); /** * @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. * ============================================================================= */ function confusionMatrix_(labels, predictions, numClasses) { var $labels = convertToTensor(labels, "labels", "confusionMatrix"); var $predictions = convertToTensor(predictions, "predictions", "confusionMatrix"); assert(numClasses == null || numClasses > 0 && Number.isInteger(numClasses), function() { return "If provided, numClasses must be a positive integer, " + ("but got " + numClasses); }); assert($labels.rank === 1, function() { return "Expected the rank of labels to be 1, but got " + $labels.rank; }); assert($predictions.rank === 1, function() { return "Expected the rank of predictions to be 1, " + ("but got " + $predictions.rank); }); assert($labels.shape[0] === $predictions.shape[0], function() { return "Mismatch in the number of examples: " + ($labels.shape[0] + " vs. " + $predictions.shape[0] + ". ") + "Labels and predictions should have the same number of elements."; }); assert(numClasses > 0 && Number.isInteger(numClasses), function() { return "numClasses is required to be a positive integer, but got " + ("" + numClasses); }); var oneHotLabels = oneHot2(cast2($labels, "int32"), numClasses); var oneHotPredictions = oneHot2(cast2($predictions, "int32"), numClasses); var oneHotLabelsT = transpose2(oneHotLabels); var product = matMul(oneHotLabelsT, oneHotPredictions); return cast2(product, "int32"); } var confusionMatrix = op({confusionMatrix_}); /** * @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 math = { __proto__: null, confusionMatrix }; /** * @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. * ============================================================================= */ function tensor3d(values, shape, dtype) { assertNonNull(values); if (shape != null && shape.length !== 3) { throw new Error("tensor3d() requires shape to have three numbers"); } var inferredShape = inferShape(values, dtype); 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"); } return makeTensor(values, shape, inferredShape, dtype); } /** * @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ var fromPixels2DContext; function fromPixels_(pixels, numChannels) { if (numChannels === void 0) { numChannels = 3; } if (numChannels > 4) { throw new Error("Cannot construct Tensor with more than 4 channels from pixels."); } if (pixels == null) { throw new Error("pixels passed to tf.browser.fromPixels() can not be null"); } var isPixelData = false; var isImageData = false; var isVideo = false; var isImage = false; var isCanvasLike = false; if (pixels.data instanceof Uint8Array) { isPixelData = true; } else if (typeof ImageData !== "undefined" && pixels instanceof ImageData) { isImageData = true; } else if (typeof HTMLVideoElement !== "undefined" && pixels instanceof HTMLVideoElement) { isVideo = true; } else if (typeof HTMLImageElement !== "undefined" && pixels instanceof HTMLImageElement) { isImage = true; } else if (pixels.getContext != null) { isCanvasLike = true; } else { throw new Error("pixels passed to tf.browser.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement, ImageData in browser, or OffscreenCanvas, ImageData in webworker or {data: Uint32Array, width: number, height: number}, " + ("but was " + pixels.constructor.name)); } if (isVideo) { var HAVE_CURRENT_DATA_READY_STATE = 2; if (isVideo && pixels.readyState < HAVE_CURRENT_DATA_READY_STATE) { throw new Error("The video element has not loaded data yet. Please wait for `loadeddata` event on the