"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @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 tf = require("../index");
var jasmine_util_1 = require("../jasmine_util");
var test_util_1 = require("../test_util");
jasmine_util_1.describeWithFlags('loadWeights', jasmine_util_1.BROWSER_ENVS, function () {
    var setupFakeWeightFiles = function (fileBufferMap) {
        spyOn(tf.env().platform, 'fetch').and.callFake(function (path) {
            return new Response(fileBufferMap[path], { headers: { 'Content-type': 'application/octet-stream' } });
        });
    };
    it('1 group, 1 weight, 1 requested weight', function () { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weightsNamesToFetch, weights, weightNames, weight0, _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    setupFakeWeightFiles({ './weightfile0': new Float32Array([1, 2, 3]) });
                    manifest = [{
                            'paths': ['weightfile0'],
                            'weights': [{ 'name': 'weight0', 'dtype': 'float32', 'shape': [3] }]
                        }];
                    weightsNamesToFetch = ['weight0'];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', weightsNamesToFetch)];
                case 1:
                    weights = _b.sent();
                    expect(tf.env().platform.fetch.calls.count()).toBe(1);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(weightsNamesToFetch.length);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_b.sent(), [1, 2, 3]]);
                    expect(weight0.shape).toEqual([3]);
                    expect(weight0.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
    it('1 group, 2 weights, fetch 1st weight', function () { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weights, weightNames, weight0, _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    setupFakeWeightFiles({ './weightfile0': new Float32Array([1, 2, 3, 4, 5]) });
                    manifest = [{
                            'paths': ['weightfile0'],
                            'weights': [
                                { 'name': 'weight0', 'dtype': 'float32', 'shape': [2] },
                                { 'name': 'weight1', 'dtype': 'float32', 'shape': [3] }
                            ]
                        }];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', ['weight0'])];
                case 1:
                    weights = _b.sent();
                    expect(tf.env().platform.fetch.calls.count()).toBe(1);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(1);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_b.sent(), [1, 2]]);
                    expect(weight0.shape).toEqual([2]);
                    expect(weight0.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
    it('1 group, 2 weights, fetch 2nd weight', function () { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weights, weightNames, weight1, _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    setupFakeWeightFiles({ './weightfile0': new Float32Array([1, 2, 3, 4, 5]) });
                    manifest = [{
                            'paths': ['weightfile0'],
                            'weights': [
                                { 'name': 'weight0', 'dtype': 'float32', 'shape': [2] },
                                { 'name': 'weight1', 'dtype': 'float32', 'shape': [3] }
                            ]
                        }];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', ['weight1'])];
                case 1:
                    weights = _b.sent();
                    expect(tf.env().platform.fetch.calls.count()).toBe(1);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(1);
                    weight1 = weights['weight1'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight1.data()];
                case 2:
                    _a.apply(void 0, [_b.sent(), [3, 4, 5]]);
                    expect(weight1.shape).toEqual([3]);
                    expect(weight1.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
    it('1 group, 2 weights, fetch all weights', function () { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weights, weightNames, weight0, _a, weight1, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    setupFakeWeightFiles({ './weightfile0': new Float32Array([1, 2, 3, 4, 5]) });
                    manifest = [{
                            'paths': ['weightfile0'],
                            'weights': [
                                { 'name': 'weight0', 'dtype': 'float32', 'shape': [2] },
                                { 'name': 'weight1', 'dtype': 'float32', 'shape': [3] }
                            ]
                        }];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', ['weight0', 'weight1'])];
                case 1:
                    weights = _c.sent();
                    expect(tf.env().platform.fetch.calls.count()).toBe(1);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(2);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_c.sent(), [1, 2]]);
                    expect(weight0.shape).toEqual([2]);
                    expect(weight0.dtype).toEqual('float32');
                    weight1 = weights['weight1'];
                    _b = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight1.data()];
                case 3:
                    _b.apply(void 0, [_c.sent(), [3, 4, 5]]);
                    expect(weight1.shape).toEqual([3]);
                    expect(weight1.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
    it('1 group, multiple weights, different dtypes', function () { return __awaiter(_this, void 0, void 0, function () {
        var buffer, view, manifest, weights, weightNames, weight0, _a, weight1, _b, weight2, _c;
        return __generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    buffer = new ArrayBuffer(5 * 4 + 1);
                    view = new DataView(buffer);
                    view.setInt32(0, 1, true);
                    view.setInt32(4, 2, true);
                    view.setUint8(8, 1);
                    view.setFloat32(9, 3., true);
                    view.setFloat32(13, 4., true);
                    view.setFloat32(17, 5., true);
                    setupFakeWeightFiles({ './weightfile0': buffer });
                    manifest = [{
                            'paths': ['weightfile0'],
                            'weights': [
                                { 'name': 'weight0', 'dtype': 'int32', 'shape': [2] },
                                { 'name': 'weight1', 'dtype': 'bool', 'shape': [] },
                                { 'name': 'weight2', 'dtype': 'float32', 'shape': [3] },
                            ]
                        }];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', ['weight0', 'weight1', 'weight2'])];
                case 1:
                    weights = _d.sent();
                    expect(tf.env().platform.fetch.calls.count()).toBe(1);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(3);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_d.sent(), [1, 2]]);
                    expect(weight0.shape).toEqual([2]);
                    expect(weight0.dtype).toEqual('int32');
                    weight1 = weights['weight1'];
                    _b = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight1.data()];
                case 3:
                    _b.apply(void 0, [_d.sent(), [1]]);
                    expect(weight1.shape).toEqual([]);
                    expect(weight1.dtype).toEqual('bool');
                    weight2 = weights['weight2'];
                    _c = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight2.data()];
                case 4:
                    _c.apply(void 0, [_d.sent(), [3, 4, 5]]);
                    expect(weight2.shape).toEqual([3]);
                    expect(weight2.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
    it('1 group, sharded 1 weight across multiple files', function () { return __awaiter(_this, void 0, void 0, function () {
        var shard0, shard1, shard2, manifest, weights, weightNames, weight0, _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    shard0 = new Float32Array([1, 2, 3, 4, 5]);
                    shard1 = new Float32Array([1.1, 2.2]);
                    shard2 = new Float32Array([10, 20, 30]);
                    setupFakeWeightFiles({
                        './weightfile0': shard0,
                        './weightsfile1': shard1,
                        './weightsfile2': shard2
                    });
                    manifest = [{
                            'paths': ['weightfile0', 'weightsfile1', 'weightsfile2'],
                            'weights': [{ 'name': 'weight0', 'dtype': 'float32', 'shape': [5, 2] }]
                        }];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', ['weight0'])];
                case 1:
                    weights = _b.sent();
                    expect(tf.env().platform.fetch.calls.count()).toBe(3);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(1);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_b.sent(), [1, 2, 3, 4, 5, 1.1, 2.2, 10, 20, 30]]);
                    expect(weight0.shape).toEqual([5, 2]);
                    expect(weight0.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
    it('1 group, sharded 2 weights across multiple files', function () { return __awaiter(_this, void 0, void 0, function () {
        var shard0, shard1, intBuffer, floatBuffer, shard2, manifest, weights, weightNames, weight0, _a, weight1, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    shard0 = new Int32Array([1, 2, 3, 4, 5]);
                    shard1 = new ArrayBuffer(5 * 4);
                    intBuffer = new Int32Array(shard1, 0, 2);
                    intBuffer.set([10, 20]);
                    floatBuffer = new Float32Array(shard1, intBuffer.byteLength, 3);
                    floatBuffer.set([3.0, 4.0, 5.0]);
                    shard2 = new Float32Array([10, 20, 30]);
                    setupFakeWeightFiles({
                        './weightfile0': shard0,
                        './weightsfile1': shard1,
                        './weightsfile2': shard2
                    });
                    manifest = [{
                            'paths': ['weightfile0', 'weightsfile1', 'weightsfile2'],
                            'weights': [
                                { 'name': 'weight0', 'dtype': 'int32', 'shape': [7, 1] },
                                { 'name': 'weight1', 'dtype': 'float32', 'shape': [3, 2] }
                            ]
                        }];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', ['weight0', 'weight1'])];
                case 1:
                    weights = _c.sent();
                    expect(tf.env().platform.fetch.calls.count()).toBe(3);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(2);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_c.sent(), [1, 2, 3, 4, 5, 10, 20]]);
                    expect(weight0.shape).toEqual([7, 1]);
                    expect(weight0.dtype).toEqual('int32');
                    weight1 = weights['weight1'];
                    _b = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight1.data()];
                case 3:
                    _b.apply(void 0, [_c.sent(), [3.0, 4.0, 5.0, 10, 20, 30]]);
                    expect(weight1.shape).toEqual([3, 2]);
                    expect(weight1.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
    it('2 group, 4 weights, fetches one group', function () { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weights, weightNames, weight0, _a, weight1, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    setupFakeWeightFiles({
                        './weightfile0': new Float32Array([1, 2, 3, 4, 5]),
                        './weightfile1': new Float32Array([6, 7, 8, 9])
                    });
                    manifest = [
                        {
                            'paths': ['weightfile0'],
                            'weights': [
                                { 'name': 'weight0', 'dtype': 'float32', 'shape': [2] },
                                { 'name': 'weight1', 'dtype': 'float32', 'shape': [3] }
                            ]
                        },
                        {
                            'paths': ['weightfile1'],
                            'weights': [
                                { 'name': 'weight2', 'dtype': 'float32', 'shape': [3, 1] },
                                { 'name': 'weight3', 'dtype': 'float32', 'shape': [] }
                            ]
                        }
                    ];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', ['weight0', 'weight1'])];
                case 1:
                    weights = _c.sent();
                    // Only the first group should be fetched.
                    expect(tf.env().platform.fetch.calls.count()).toBe(1);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(2);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_c.sent(), [1, 2]]);
                    expect(weight0.shape).toEqual([2]);
                    expect(weight0.dtype).toEqual('float32');
                    weight1 = weights['weight1'];
                    _b = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight1.data()];
                case 3:
                    _b.apply(void 0, [_c.sent(), [3, 4, 5]]);
                    expect(weight1.shape).toEqual([3]);
                    expect(weight1.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
    it('2 group, 4 weights, one weight from each group', function () { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weights, weightNames, weight0, _a, weight2, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    setupFakeWeightFiles({
                        './weightfile0': new Float32Array([1, 2, 3, 4, 5]),
                        './weightfile1': new Float32Array([6, 7, 8, 9])
                    });
                    manifest = [
                        {
                            'paths': ['weightfile0'],
                            'weights': [
                                { 'name': 'weight0', 'dtype': 'float32', 'shape': [2] },
                                { 'name': 'weight1', 'dtype': 'float32', 'shape': [3] }
                            ]
                        },
                        {
                            'paths': ['weightfile1'],
                            'weights': [
                                { 'name': 'weight2', 'dtype': 'float32', 'shape': [3, 1] },
                                { 'name': 'weight3', 'dtype': 'float32', 'shape': [] }
                            ]
                        }
                    ];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', ['weight0', 'weight2'])];
                case 1:
                    weights = _c.sent();
                    // Both groups need to be fetched.
                    expect(tf.env().platform.fetch.calls.count()).toBe(2);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(2);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_c.sent(), [1, 2]]);
                    expect(weight0.shape).toEqual([2]);
                    expect(weight0.dtype).toEqual('float32');
                    weight2 = weights['weight2'];
                    _b = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight2.data()];
                case 3:
                    _b.apply(void 0, [_c.sent(), [6, 7, 8]]);
                    expect(weight2.shape).toEqual([3, 1]);
                    expect(weight2.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
    it('2 group, 4 weights, dont specify weights fetchs all', function () { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weights, weightNames, weight0, _a, weight1, _b, weight2, _c, weight3, _d;
        return __generator(this, function (_e) {
            switch (_e.label) {
                case 0:
                    setupFakeWeightFiles({
                        './weightfile0': new Float32Array([1, 2, 3, 4, 5]),
                        './weightfile1': new Float32Array([6, 7, 8, 9])
                    });
                    manifest = [
                        {
                            'paths': ['weightfile0'],
                            'weights': [
                                { 'name': 'weight0', 'dtype': 'float32', 'shape': [2] },
                                { 'name': 'weight1', 'dtype': 'float32', 'shape': [3] }
                            ]
                        },
                        {
                            'paths': ['weightfile1'],
                            'weights': [
                                { 'name': 'weight2', 'dtype': 'float32', 'shape': [3, 1] },
                                { 'name': 'weight3', 'dtype': 'float32', 'shape': [] }
                            ]
                        }
                    ];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './')];
                case 1:
                    weights = _e.sent();
                    // Both groups need to be fetched.
                    expect(tf.env().platform.fetch.calls.count()).toBe(2);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(4);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_e.sent(), [1, 2]]);
                    expect(weight0.shape).toEqual([2]);
                    expect(weight0.dtype).toEqual('float32');
                    weight1 = weights['weight1'];
                    _b = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight1.data()];
                case 3:
                    _b.apply(void 0, [_e.sent(), [3, 4, 5]]);
                    expect(weight1.shape).toEqual([3]);
                    expect(weight1.dtype).toEqual('float32');
                    weight2 = weights['weight2'];
                    _c = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight2.data()];
                case 4:
                    _c.apply(void 0, [_e.sent(), [6, 7, 8]]);
                    expect(weight2.shape).toEqual([3, 1]);
                    expect(weight2.dtype).toEqual('float32');
                    weight3 = weights['weight3'];
                    _d = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight3.data()];
                case 5:
                    _d.apply(void 0, [_e.sent(), [9]]);
                    expect(weight3.shape).toEqual([]);
                    expect(weight3.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
    it('throws if requested weight not found', function (done) { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weightsNamesToFetch, e_1;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    setupFakeWeightFiles({ './weightfile0': new Float32Array([1, 2, 3]) });
                    manifest = [{
                            'paths': ['weightfile0'],
                            'weights': [{ 'name': 'weight0', 'dtype': 'float32', 'shape': [3] }]
                        }];
                    weightsNamesToFetch = ['doesntexist'];
                    _a.label = 1;
                case 1:
                    _a.trys.push([1, 3, , 4]);
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', weightsNamesToFetch)];
                case 2:
                    _a.sent();
                    done.fail();
                    return [3 /*break*/, 4];
                case 3:
                    e_1 = _a.sent();
                    done();
                    return [3 /*break*/, 4];
                case 4: return [2 /*return*/];
            }
        });
    }); });
    it('throws if requested weight has unknown dtype', function (done) { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weightsNamesToFetch, e_2;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    setupFakeWeightFiles({ './weightfile0': new Float32Array([1, 2, 3]) });
                    manifest = [{
                            'paths': ['weightfile0'],
                            'weights': [{
                                    'name': 'weight0',
                                    // tslint:disable-next-line:no-any
                                    'dtype': 'null',
                                    'shape': [3]
                                }]
                        }];
                    weightsNamesToFetch = ['weight0'];
                    _a.label = 1;
                case 1:
                    _a.trys.push([1, 3, , 4]);
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', weightsNamesToFetch)];
                case 2:
                    _a.sent();
                    done.fail();
                    return [3 /*break*/, 4];
                case 3:
                    e_2 = _a.sent();
                    done();
                    return [3 /*break*/, 4];
                case 4: return [2 /*return*/];
            }
        });
    }); });
    it('should use request option', function () { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weightsNamesToFetch;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    setupFakeWeightFiles({ './weightfile0': new Float32Array([1, 2, 3]) });
                    manifest = [{
                            'paths': ['weightfile0'],
                            'weights': [{ 'name': 'weight0', 'dtype': 'float32', 'shape': [3] }]
                        }];
                    weightsNamesToFetch = ['weight0'];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', weightsNamesToFetch, { credentials: 'include' })];
                case 1:
                    _a.sent();
                    expect(tf.env().platform.fetch.calls.count()).toBe(1);
                    expect(tf.env().platform.fetch)
                        .toHaveBeenCalledWith('./weightfile0', { credentials: 'include' }, { isBinary: true });
                    return [2 /*return*/];
            }
        });
    }); });
    var quantizationTest = function (quantizationDtype) { return __awaiter(_this, void 0, void 0, function () {
        var arrayType, manifest, weightsNamesToFetch, weights, weightNames, weight0, _a, weight1, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    arrayType = quantizationDtype === 'uint8' ? Uint8Array : Uint16Array;
                    setupFakeWeightFiles({ './weightfile0': new arrayType([0, 48, 255, 0, 48, 255]) });
                    manifest = [{
                            'paths': ['weightfile0'],
                            'weights': [
                                {
                                    'name': 'weight0',
                                    'dtype': 'float32',
                                    'shape': [3],
                                    'quantization': { 'min': -1, 'scale': 0.1, 'dtype': quantizationDtype }
                                },
                                {
                                    'name': 'weight1',
                                    'dtype': 'int32',
                                    'shape': [3],
                                    'quantization': { 'min': -1, 'scale': 0.1, 'dtype': quantizationDtype }
                                }
                            ]
                        }];
                    weightsNamesToFetch = ['weight0', 'weight1'];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', weightsNamesToFetch)];
                case 1:
                    weights = _c.sent();
                    expect(tf.env().platform.fetch.calls.count()).toBe(1);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(weightsNamesToFetch.length);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_c.sent(), [-1, 3.8, 24.5]]);
                    expect(weight0.shape).toEqual([3]);
                    expect(weight0.dtype).toEqual('float32');
                    weight1 = weights['weight1'];
                    _b = test_util_1.expectArraysEqual;
                    return [4 /*yield*/, weight1.data()];
                case 3:
                    _b.apply(void 0, [_c.sent(), [-1, 4, 25]]);
                    expect(weight1.shape).toEqual([3]);
                    expect(weight1.dtype).toEqual('int32');
                    return [2 /*return*/];
            }
        });
    }); };
    it('quantized weights (uint8)', function () { return __awaiter(_this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, quantizationTest('uint8')];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }); });
    it('quantized weights (uint16)', function () { return __awaiter(_this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, quantizationTest('uint16')];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }); });
    it('2 groups, 1 quantized, 1 unquantized', function () { return __awaiter(_this, void 0, void 0, function () {
        var manifest, weights, weightNames, weight0, _a, weight2, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    setupFakeWeightFiles({
                        './weightfile0': new Uint8Array([0, 48, 255, 0, 48, 255]),
                        './weightfile1': new Float32Array([6, 7, 8, 9])
                    });
                    manifest = [
                        {
                            'paths': ['weightfile0'],
                            'weights': [
                                {
                                    'name': 'weight0',
                                    'dtype': 'float32',
                                    'shape': [3],
                                    'quantization': { 'min': -1, 'scale': 0.1, 'dtype': 'uint8' }
                                },
                                {
                                    'name': 'weight1',
                                    'dtype': 'int32',
                                    'shape': [3],
                                    'quantization': { 'min': -1, 'scale': 0.1, 'dtype': 'uint8' }
                                }
                            ]
                        },
                        {
                            'paths': ['weightfile1'],
                            'weights': [
                                { 'name': 'weight2', 'dtype': 'float32', 'shape': [3, 1] },
                                { 'name': 'weight3', 'dtype': 'float32', 'shape': [] }
                            ]
                        }
                    ];
                    return [4 /*yield*/, tf.io.loadWeights(manifest, './', ['weight0', 'weight2'])];
                case 1:
                    weights = _c.sent();
                    // Both groups need to be fetched.
                    expect(tf.env().platform.fetch.calls.count()).toBe(2);
                    weightNames = Object.keys(weights);
                    expect(weightNames.length).toEqual(2);
                    weight0 = weights['weight0'];
                    _a = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight0.data()];
                case 2:
                    _a.apply(void 0, [_c.sent(), [-1, 3.8, 24.5]]);
                    expect(weight0.shape).toEqual([3]);
                    expect(weight0.dtype).toEqual('float32');
                    weight2 = weights['weight2'];
                    _b = test_util_1.expectArraysClose;
                    return [4 /*yield*/, weight2.data()];
                case 3:
                    _b.apply(void 0, [_c.sent(), [6, 7, 8]]);
                    expect(weight2.shape).toEqual([3, 1]);
                    expect(weight2.dtype).toEqual('float32');
                    return [2 /*return*/];
            }
        });
    }); });
});
//# sourceMappingURL=weights_loader_test.js.map