New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

deeplearn

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deeplearn - npm Package Compare versions

Comparing version 0.3.13 to 0.3.14

dist/math/backends/backend_engine.d.ts

3

dist/data/checkpoint_loader.js

@@ -75,2 +75,5 @@ "use strict";

xhr.onload = function () {
if (xhr.status === 404) {
throw new Error("Not found variable " + varName);
}
var values = new Float32Array(xhr.response);

@@ -77,0 +80,0 @@ var ndarray = ndarray_1.NDArray.make(_this.checkpointManifest[varName].shape, { values: values });

@@ -0,1 +1,2 @@

import { NDArrayMath } from './math/math';
export declare enum Type {

@@ -17,9 +18,13 @@ NUMBER = 0,

}
export declare type Backends = 'webgl' | 'cpu';
export declare class Environment {
private features;
private globalMath;
constructor(features?: Features);
get<K extends keyof Features>(feature: K): Features[K];
private evaluateFeature<K>(feature);
setGlobalMath(math: NDArrayMath): void;
readonly math: NDArrayMath;
}
export declare let ENV: Environment;
export declare function setEnvironment(environment: Environment): void;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var device_util = require("./device_util");
var backend_1 = require("./math/backends/backend");
var math_1 = require("./math/math");
var util = require("./util");

@@ -96,5 +98,16 @@ var Type;

}
function getBestBackend() {
var orderedBackends = ['webgl', 'cpu'];
for (var i = 0; i < orderedBackends.length; ++i) {
var backendId = orderedBackends[i];
if (backendId in backend_1.BACKEND_REGISTRY) {
return backendId;
}
}
throw new Error('No backend found in registry.');
}
var Environment = (function () {
function Environment(features) {
this.features = {};
this.globalMath = null;
if (features != null) {

@@ -140,2 +153,17 @@ this.features = features;

};
Environment.prototype.setGlobalMath = function (math) {
this.globalMath = math;
};
Object.defineProperty(Environment.prototype, "math", {
get: function () {
if (this.globalMath == null) {
var bestBackend = getBestBackend();
var safeMode = false;
this.globalMath = new math_1.NDArrayMath(bestBackend, safeMode);
}
return this.globalMath;
},
enumerable: true,
configurable: true
});
return Environment;

@@ -142,0 +170,0 @@ }());

6

dist/graph/ops/matmul.js

@@ -13,3 +13,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var backend_1 = require("../../math/backends/backend");
var matmul_1 = require("../../math/backends/types/matmul");
var graph_util = require("../graph_util");

@@ -57,7 +57,7 @@ var op_1 = require("./op");

if (graph_util.shouldBackProp(_this.x1Tensor)) {
var dx1 = math.matMul(dy, x2, backend_1.MatrixOrientation.REGULAR, backend_1.MatrixOrientation.TRANSPOSED);
var dx1 = math.matMul(dy, x2, matmul_1.MatrixOrientation.REGULAR, matmul_1.MatrixOrientation.TRANSPOSED);
gradientArrays.add(_this.x1Tensor, _this.x1Tensor.shape.length === 1 ? dx1.as1D() : dx1);
}
if (graph_util.shouldBackProp(_this.x2Tensor)) {
var dx2 = math.matMul(x1, dy, backend_1.MatrixOrientation.TRANSPOSED, backend_1.MatrixOrientation.REGULAR);
var dx2 = math.matMul(x1, dy, matmul_1.MatrixOrientation.TRANSPOSED, matmul_1.MatrixOrientation.REGULAR);
gradientArrays.add(_this.x2Tensor, _this.x2Tensor.shape.length === 1 ? dx2.as1D() : dx2);

@@ -64,0 +64,0 @@ }

@@ -24,2 +24,3 @@ "use strict";

util.assertShapesMatch(outTensor.shape, []);
_this.ones = ndarray_1.NDArray.ones(x.shape);
return _this;

@@ -41,7 +42,2 @@ }

var dy = gradientArrays.get(_this.outTensor);
if (_this.ones == null) {
var xArray = inferenceArrays.get(_this.x);
_this.ones = ndarray_1.NDArray.zerosLike(xArray);
_this.ones.fill(1);
}
gradientArrays.add(_this.x, math.scalarTimesArray(dy, _this.ones));

@@ -48,0 +44,0 @@ });

@@ -27,8 +27,7 @@ import * as xhr_dataset from './data/xhr-dataset';

export { ConstantInitializer, Initializer, NDArrayInitializer, OnesInitializer, RandomNormalInitializer, RandomTruncatedNormalInitializer, RandomUniformInitializer, VarianceScalingInitializer, ZerosInitializer } from './initializers';
export { MatrixOrientation } from './math/backends/backend';
export { NDArrayMathCPU } from './math/backends/backend_cpu';
export { NDArrayMathGPU } from './math/backends/backend_webgl';
export { MatrixOrientation } from './math/backends/types/matmul';
export { GPGPUContext } from './math/backends/webgl/gpgpu_context';
export { LSTMCell, NDArrayMath } from './math/math';
export { initializeGPU } from './math/ndarray';
export { Array1D, Array2D, Array3D, Array4D, NDArray, Scalar } from './math/ndarray';

@@ -35,0 +34,0 @@ export { Model } from './model';

@@ -67,4 +67,2 @@ "use strict";

exports.ZerosInitializer = initializers_1.ZerosInitializer;
var backend_1 = require("./math/backends/backend");
exports.MatrixOrientation = backend_1.MatrixOrientation;
var backend_cpu_1 = require("./math/backends/backend_cpu");

@@ -74,2 +72,4 @@ exports.NDArrayMathCPU = backend_cpu_1.NDArrayMathCPU;

exports.NDArrayMathGPU = backend_webgl_1.NDArrayMathGPU;
var matmul_1 = require("./math/backends/types/matmul");
exports.MatrixOrientation = matmul_1.MatrixOrientation;
var gpgpu_context_1 = require("./math/backends/webgl/gpgpu_context");

@@ -80,9 +80,7 @@ exports.GPGPUContext = gpgpu_context_1.GPGPUContext;

var ndarray_1 = require("./math/ndarray");
exports.initializeGPU = ndarray_1.initializeGPU;
var ndarray_2 = require("./math/ndarray");
exports.Array1D = ndarray_2.Array1D;
exports.Array2D = ndarray_2.Array2D;
exports.Array3D = ndarray_2.Array3D;
exports.Array4D = ndarray_2.Array4D;
exports.NDArray = ndarray_2.NDArray;
exports.Scalar = ndarray_2.Scalar;
exports.Array1D = ndarray_1.Array1D;
exports.Array2D = ndarray_1.Array2D;
exports.Array3D = ndarray_1.Array3D;
exports.Array4D = ndarray_1.Array4D;
exports.NDArray = ndarray_1.NDArray;
exports.Scalar = ndarray_1.Scalar;

@@ -9,2 +9,3 @@ import { NDArrayMath } from './math';

export declare class TanHFunc implements ActivationFunction {
private one;
output<T extends NDArray>(math: NDArrayMath, x: T): T;

@@ -32,2 +33,3 @@ der<T extends NDArray>(math: NDArrayMath, x: T, y: T): T;

export declare class SquareFunc implements ActivationFunction {
private two;
output<T extends NDArray>(math: NDArrayMath, x: T): T;

@@ -34,0 +36,0 @@ der<T extends NDArray>(math: NDArrayMath, x: T, y: T): T;

@@ -6,15 +6,17 @@ "use strict";

function TanHFunc() {
this.one = ndarray_1.Scalar.new(1);
}
TanHFunc.prototype.output = function (math, x) {
return math.scope(function () {
return math.tanh(x);
});
return math.tanh(x);
};
TanHFunc.prototype.der = function (math, x, y) {
var _this = this;
return math.scope(function () {
var ySquared = math.elementWiseMul(y, y);
return math.scalarMinusArray(ndarray_1.Scalar.ONE, ySquared);
return math.scalarMinusArray(_this.one, ySquared);
});
};
TanHFunc.prototype.dispose = function () { };
TanHFunc.prototype.dispose = function () {
this.one.dispose();
};
return TanHFunc;

@@ -27,10 +29,6 @@ }());

ReLUFunc.prototype.output = function (math, x) {
return math.scope(function () {
return math.relu(x);
});
return math.relu(x);
};
ReLUFunc.prototype.der = function (math, x, y) {
return math.scope(function () {
return math.step(x);
});
return math.step(x);
};

@@ -59,5 +57,3 @@ ReLUFunc.prototype.dispose = function () { };

SigmoidFunc.prototype.output = function (math, x) {
return math.scope(function () {
return math.sigmoid(x);
});
return math.sigmoid(x);
};

@@ -76,14 +72,13 @@ SigmoidFunc.prototype.der = function (math, x, y) {

function SquareFunc() {
this.two = ndarray_1.Scalar.new(2);
}
SquareFunc.prototype.output = function (math, x) {
return math.scope(function () {
return math.elementWiseMul(x, x);
});
return math.elementWiseMul(x, x);
};
SquareFunc.prototype.der = function (math, x, y) {
return math.scope(function () {
return math.scalarTimesArray(ndarray_1.Scalar.TWO, x);
});
return math.scalarTimesArray(this.two, x);
};
SquareFunc.prototype.dispose = function () { };
SquareFunc.prototype.dispose = function () {
this.two.dispose();
};
return SquareFunc;

@@ -90,0 +85,0 @@ }());

import { Conv2DInfo } from '../conv_util';
import { NDArrayMath } from '../math';
import { Array1D, Array2D, Array3D, Array4D, DataTypes, NDArray, Scalar } from '../ndarray';
import { Array1D, Array2D, Array3D, Array4D, DataTypes, NDArray } from '../ndarray';
import { SumTypes } from '../types';
import { MathBackend, MatrixOrientation } from './backend';
import { MathBackend } from './backend';
import { MatrixOrientation } from './types/matmul';
export declare class MathBackendCPU implements MathBackend {
clone<T extends NDArray>(ndarray: T): T;
slice1D(input: Array1D, begin: number, size: number): Array1D;
slice2D(input: Array2D, begin: [number, number], size: [number, number]): Array2D;
slice3D(input: Array3D, begin: [number, number, number], size: [number, number, number]): Array3D;
slice4D(input: Array4D, begin: [number, number, number, number], size: [number, number, number, number]): Array4D;
copy2D(source: Array2D, sourceBeginRowCol: [number, number], sourceSizeRowCol: [number, number], dest: Array2D, destBeginRowCol: [number, number], destSizeRowCol: [number, number]): void;
private data;
dispose(): void;
write<T extends keyof DataTypes>(id: number, values: DataTypes[T], dtype: T, shape: number[]): void;
writePixels(id: number, pixels: ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement, numChannels: number): void;
readSync<T extends keyof DataTypes>(id: number): DataTypes[T];
disposeData(id: number): void;
read<T extends keyof DataTypes>(id: number): Promise<DataTypes[T]>;
clone<T extends NDArray>(x: T): T;
slice1D(x: Array1D, begin: number, size: number): Array1D;
slice2D(x: Array2D, begin: [number, number], size: [number, number]): Array2D;
slice3D(x: Array3D, begin: [number, number, number], size: [number, number, number]): Array3D;
slice4D(x: Array4D, begin: [number, number, number, number], size: [number, number, number, number]): Array4D;
concat1D(a: Array1D, b: Array1D): Array1D;

@@ -17,49 +24,50 @@ concat2D(a: Array2D, b: Array2D, axis: number): Array2D;

concat4D(a: Array4D, b: Array4D, axis: number): Array4D;
scaledArrayAdd<T extends NDArray>(c1: Scalar, a: T, c2: Scalar, b: T): T;
neg<T extends NDArray>(a: T): T;
private scaledArrayAdd<T>(c1, a, c2, b);
neg<T extends NDArray>(x: T): T;
add<T extends NDArray>(a: T, b: T): T;
subtract<T extends NDArray>(a: T, b: T): T;
pow<T extends NDArray>(a: T, b: NDArray<'int32'>): T;
matMul(a: Array2D, b: Array2D, aOrientation?: MatrixOrientation, bOrientation?: MatrixOrientation): Array2D;
multiply<T extends NDArray>(a: T, b: T): T;
divide(a: NDArray, b: NDArray): NDArray<'float32'>;
sum<T extends keyof DataTypes>(input: NDArray<T>, axes: number[]): NDArray<SumTypes[T]>;
argMin(input: NDArray, axes: number[]): NDArray<'int32'>;
argMax(input: NDArray, axes: number[]): NDArray<'int32'>;
sum<T extends keyof DataTypes>(x: NDArray<T>, axes: number[]): NDArray<SumTypes[T]>;
argMin(x: NDArray, axes: number[]): NDArray<'int32'>;
argMax(x: NDArray, axes: number[]): NDArray<'int32'>;
equal(a: NDArray, b: NDArray): NDArray<'bool'>;
topKValues<D extends keyof DataTypes, T extends NDArray<D>>(ndarray: T, k: number): Array1D<D>;
topKIndices(ndarray: NDArray, k: number): Array1D<'int32'>;
private topK<D, T>(ndarray, k);
min<G extends keyof DataTypes>(input: NDArray<G>, axes: number[]): NDArray<G>;
max<G extends keyof DataTypes>(input: NDArray<G>, axes: number[]): NDArray<G>;
ceil<T extends NDArray>(ndarray: T): T;
floor<T extends NDArray>(ndarray: T): T;
exp<T extends NDArray>(ndarray: T): T;
log<T extends NDArray>(ndarray: T): T;
sqrt<T extends NDArray>(ndarray: T): T;
topKValues<D extends keyof DataTypes, T extends NDArray<D>>(x: T, k: number): Array1D<D>;
topKIndices(x: NDArray, k: number): Array1D<'int32'>;
private topK<D, T>(x, k);
min<G extends keyof DataTypes>(x: NDArray<G>, axes: number[]): NDArray<G>;
max<G extends keyof DataTypes>(x: NDArray<G>, axes: number[]): NDArray<G>;
ceil<T extends NDArray>(x: T): T;
floor<T extends NDArray>(x: T): T;
exp<T extends NDArray>(x: T): T;
log<T extends NDArray>(x: T): T;
sqrt<T extends NDArray>(x: T): T;
square<T extends NDArray>(x: T): T;
relu<T extends NDArray>(input: T): T;
elu<T extends NDArray>(ndarray: T): T;
eluDer<T extends NDArray>(ndarray: T): T;
selu<T extends NDArray>(ndarray: T): T;
leakyRelu<T extends NDArray>(ndarray: T, alpha: number): T;
clip<T extends NDArray>(ndarray: T, min: number, max: number): T;
abs<T extends NDArray>(ndarray: T): T;
sigmoid<T extends NDArray>(ndarray: T): T;
sin<T extends NDArray>(ndarray: T): T;
cos<T extends NDArray>(ndarray: T): T;
tan<T extends NDArray>(ndarray: T): T;
asin<T extends NDArray>(ndarray: T): T;
acos<T extends NDArray>(ndarray: T): T;
atan<T extends NDArray>(ndarray: T): T;
sinh<T extends NDArray>(ndarray: T): T;
cosh<T extends NDArray>(ndarray: T): T;
tanh<T extends NDArray>(ndarray: T): T;
step<T extends NDArray>(ndarray: T, alpha?: number): T;
relu<T extends NDArray>(x: T): T;
elu<T extends NDArray>(x: T): T;
eluDer<T extends NDArray>(x: T): T;
selu<T extends NDArray>(x: T): T;
leakyRelu<T extends NDArray>(x: T, alpha: number): T;
clip<T extends NDArray>(x: T, min: number, max: number): T;
abs<T extends NDArray>(x: T): T;
sigmoid<T extends NDArray>(x: T): T;
sin<T extends NDArray>(x: T): T;
cos<T extends NDArray>(x: T): T;
tan<T extends NDArray>(x: T): T;
asin<T extends NDArray>(x: T): T;
acos<T extends NDArray>(x: T): T;
atan<T extends NDArray>(x: T): T;
sinh<T extends NDArray>(x: T): T;
cosh<T extends NDArray>(x: T): T;
tanh<T extends NDArray>(x: T): T;
step<T extends NDArray>(x: T, alpha?: number): T;
conv2d(x: Array4D, filter: Array4D, bias: Array1D | null, convInfo: Conv2DInfo): Array4D;
conv2dDerInput(dy: Array4D, filter: Array4D, convInfo: Conv2DInfo): Array4D;
conv2dDerFilter(x: Array4D, dY: Array4D, convInfo: Conv2DInfo): Array4D;
conv2dDerBias(dY: Array4D): Array1D;
depthwiseConv2D(input: Array4D, filter: Array4D, convInfo: Conv2DInfo): Array4D;
tile<D extends keyof DataTypes, T extends NDArray<D>>(a: T, reps: number[]): T;
transpose<D extends keyof DataTypes, T extends NDArray<D>>(a: T, perm: number[]): T;
conv2dDerFilter(x: Array4D, dy: Array4D, convInfo: Conv2DInfo): Array4D;
conv2dDerBias(dy: Array4D): Array1D;
depthwiseConv2D(x: Array4D, filter: Array4D, convInfo: Conv2DInfo): Array4D;
tile<D extends keyof DataTypes, T extends NDArray<D>>(x: T, reps: number[]): T;
transpose<D extends keyof DataTypes, T extends NDArray<D>>(x: T, perm: number[]): T;
private pool(x, convInfo, poolType);

@@ -66,0 +74,0 @@ maxPool(x: Array4D, convInfo: Conv2DInfo): Array4D;

@@ -12,2 +12,37 @@ "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 = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -18,3 +53,2 @@ var seedrandom = require("seedrandom");

var concat_util = require("../concat_util");
var copy2D_util = require("../copy2d_util");
var math_1 = require("../math");

@@ -25,13 +59,70 @@ var ndarray_1 = require("../ndarray");

var backend_1 = require("./backend");
var matmul_1 = require("./types/matmul");
var MathBackendCPU = (function () {
function MathBackendCPU() {
this.data = {};
}
MathBackendCPU.prototype.clone = function (ndarray) {
return ndarray_1.NDArray.make(ndarray.shape, { values: new Float32Array(ndarray.getValues()) });
MathBackendCPU.prototype.dispose = function () { };
MathBackendCPU.prototype.write = function (id, values, dtype, shape) {
this.data[id] = values;
};
MathBackendCPU.prototype.slice1D = function (input, begin, size) {
var newVals = input.getValues().slice(begin, begin + size);
MathBackendCPU.prototype.writePixels = function (id, pixels, numChannels) {
var vals;
if (pixels instanceof ImageData) {
vals = pixels.data;
}
else if (pixels instanceof HTMLCanvasElement) {
vals = pixels.getContext('2d')
.getImageData(0, 0, pixels.width, pixels.height)
.data;
}
else if (pixels instanceof HTMLImageElement ||
pixels instanceof HTMLVideoElement) {
var canvas = document.createElement('canvas');
canvas.width = pixels.width;
canvas.height = pixels.height;
canvas.getContext('2d').drawImage(pixels, 0, 0, canvas.width, canvas.height);
vals = canvas.getContext('2d')
.getImageData(0, 0, canvas.width, canvas.height)
.data;
}
else {
throw new Error("pixels is of unknown type: " + pixels.constructor.name);
}
var values;
if (numChannels === 4) {
values = new Int32Array(vals);
}
else {
var numPixels = pixels.width * pixels.height;
values = new Int32Array(numPixels * numChannels);
for (var i = 0; i < numPixels; i++) {
for (var channel = 0; channel < numChannels; ++channel) {
values[i * numChannels + channel] = vals[i * 4 + channel];
}
}
}
this.data[id] = values;
};
MathBackendCPU.prototype.readSync = function (id) {
return this.data[id];
};
MathBackendCPU.prototype.disposeData = function (id) {
delete this.data[id];
};
MathBackendCPU.prototype.read = function (id) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, this.data[id]];
});
});
};
MathBackendCPU.prototype.clone = function (x) {
return ndarray_1.NDArray.make(x.shape, { values: new Float32Array(x.getValues()) });
};
MathBackendCPU.prototype.slice1D = function (x, begin, size) {
var newVals = x.getValues().slice(begin, begin + size);
return ndarray_1.Array1D.new(newVals);
};
MathBackendCPU.prototype.slice2D = function (input, begin, size) {
MathBackendCPU.prototype.slice2D = function (x, begin, size) {
var result = ndarray_1.Array2D.zeros(size);

@@ -41,3 +132,3 @@ var startI = begin[0], startJ = begin[1];

for (var j = 0; j < size[1]; ++j) {
var val = input.get(i + startI, j + startJ);
var val = x.get(i + startI, j + startJ);
result.set(val, i, j);

@@ -48,3 +139,3 @@ }

};
MathBackendCPU.prototype.slice3D = function (input, begin, size) {
MathBackendCPU.prototype.slice3D = function (x, begin, size) {
var result = ndarray_1.Array3D.zeros(size);

@@ -55,3 +146,3 @@ var startI = begin[0], startJ = begin[1], startK = begin[2];

for (var k = 0; k < size[2]; ++k) {
var val = input.get(i + startI, j + startJ, k + startK);
var val = x.get(i + startI, j + startJ, k + startK);
result.set(val, i, j, k);

@@ -63,3 +154,3 @@ }

};
MathBackendCPU.prototype.slice4D = function (input, begin, size) {
MathBackendCPU.prototype.slice4D = function (x, begin, size) {
var result = ndarray_1.Array4D.zeros(size);

@@ -71,3 +162,3 @@ var startI = begin[0], startJ = begin[1], startK = begin[2], startL = begin[3];

for (var l = 0; l < size[3]; ++l) {
var val = input.get(i + startI, j + startJ, k + startK, l + startL);
var val = x.get(i + startI, j + startJ, k + startK, l + startL);
result.set(val, i, j, k, l);

@@ -80,17 +171,2 @@ }

};
MathBackendCPU.prototype.copy2D = function (source, sourceBeginRowCol, sourceSizeRowCol, dest, destBeginRowCol, destSizeRowCol) {
copy2D_util.validateShapes(sourceSizeRowCol, destSizeRowCol);
var srcValues = source.getValues();
var dstValues = dest.getValues();
var n = sourceSizeRowCol[0] * sourceSizeRowCol[1];
for (var i = 0; i < n; ++i) {
var srcRow = sourceBeginRowCol[0] + Math.floor(i / sourceSizeRowCol[1]);
var srcCol = sourceBeginRowCol[1] + (i % sourceSizeRowCol[1]);
var srcOff = srcRow * source.shape[1] + srcCol;
var dstRow = destBeginRowCol[0] + Math.floor(i / destSizeRowCol[1]);
var dstCol = destBeginRowCol[1] + (i % destSizeRowCol[1]);
var dstOff = dstRow * dest.shape[1] + dstCol;
dstValues[dstOff] = srcValues[srcOff];
}
};
MathBackendCPU.prototype.concat1D = function (a, b) {

@@ -203,17 +279,28 @@ var outShape = concat_util.computeOutShape(a.shape, b.shape, 0);

};
MathBackendCPU.prototype.neg = function (a) {
return this.multiply(ndarray_1.Scalar.NEG_ONE, a);
MathBackendCPU.prototype.neg = function (x) {
return this.multiply(ndarray_1.Scalar.new(-1), x);
};
MathBackendCPU.prototype.add = function (a, b) {
return this.scaledArrayAdd(ndarray_1.Scalar.ONE, a, ndarray_1.Scalar.ONE, b);
var one = ndarray_1.Scalar.new(1);
return this.scaledArrayAdd(one, a, one, b);
};
MathBackendCPU.prototype.subtract = function (a, b) {
return this.scaledArrayAdd(ndarray_1.Scalar.ONE, a, ndarray_1.Scalar.NEG_ONE, b);
return this.scaledArrayAdd(ndarray_1.Scalar.new(1), a, ndarray_1.Scalar.new(-1), b);
};
MathBackendCPU.prototype.pow = function (a, b) {
var newShape = broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
var newValues = new Float32Array(util.sizeFromShape(newShape));
var aValues = a.getValues();
var bValues = b.getValues();
for (var i = 0; i < newValues.length; ++i) {
newValues[i] = Math.pow(aValues[i % a.size], bValues[i % b.size]);
}
return ndarray_1.NDArray.make(newShape, { values: newValues });
};
MathBackendCPU.prototype.matMul = function (a, b, aOrientation, bOrientation) {
if (aOrientation === void 0) { aOrientation = backend_1.MatrixOrientation.REGULAR; }
if (bOrientation === void 0) { bOrientation = backend_1.MatrixOrientation.REGULAR; }
var sharedDim = (aOrientation === backend_1.MatrixOrientation.REGULAR) ? a.shape[1] : a.shape[0];
var leftDim = (aOrientation === backend_1.MatrixOrientation.REGULAR) ? a.shape[0] : a.shape[1];
var rightDim = (bOrientation === backend_1.MatrixOrientation.REGULAR) ? b.shape[1] : b.shape[0];
if (aOrientation === void 0) { aOrientation = matmul_1.MatrixOrientation.REGULAR; }
if (bOrientation === void 0) { bOrientation = matmul_1.MatrixOrientation.REGULAR; }
var sharedDim = (aOrientation === matmul_1.MatrixOrientation.REGULAR) ? a.shape[1] : a.shape[0];
var leftDim = (aOrientation === matmul_1.MatrixOrientation.REGULAR) ? a.shape[0] : a.shape[1];
var rightDim = (bOrientation === matmul_1.MatrixOrientation.REGULAR) ? b.shape[1] : b.shape[0];
var normalGetter = function (matrix, i, j) {

@@ -225,6 +312,6 @@ return matrix.get(i, j);

};
var aGetter = (aOrientation === backend_1.MatrixOrientation.REGULAR) ?
var aGetter = (aOrientation === matmul_1.MatrixOrientation.REGULAR) ?
normalGetter :
transposedGetter;
var bGetter = (bOrientation === backend_1.MatrixOrientation.REGULAR) ?
var bGetter = (bOrientation === matmul_1.MatrixOrientation.REGULAR) ?
normalGetter :

@@ -265,10 +352,10 @@ transposedGetter;

};
MathBackendCPU.prototype.sum = function (input, axes) {
axis_util.assertAxesAreInnerMostDims('sum', axes, input.rank);
var _a = axis_util.computeOutAndReduceShapes(input.shape, axes), outShape = _a[0], reduceShape = _a[1];
var resultDtype = types_1.SumTypesMap[input.dtype];
MathBackendCPU.prototype.sum = function (x, axes) {
axis_util.assertAxesAreInnerMostDims('sum', axes, x.rank);
var _a = axis_util.computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
var resultDtype = types_1.SumTypesMap[x.dtype];
var result = ndarray_1.NDArray.zeros(outShape, resultDtype);
var reduceSize = util.sizeFromShape(reduceShape);
var vals = result.getValues();
var aVals = input.getValues();
var aVals = x.getValues();
for (var i = 0; i < vals.length; ++i) {

@@ -284,9 +371,9 @@ var offset = i * reduceSize;

};
MathBackendCPU.prototype.argMin = function (input, axes) {
axis_util.assertAxesAreInnerMostDims('argMin', axes, input.rank);
var _a = axis_util.computeOutAndReduceShapes(input.shape, axes), outShape = _a[0], reduceShape = _a[1];
MathBackendCPU.prototype.argMin = function (x, axes) {
axis_util.assertAxesAreInnerMostDims('argMin', axes, x.rank);
var _a = axis_util.computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
var result = ndarray_1.NDArray.zeros(outShape, 'int32');
var reduceSize = util.sizeFromShape(reduceShape);
var vals = result.getValues();
var aVals = input.getValues();
var aVals = x.getValues();
for (var i = 0; i < vals.length; ++i) {

@@ -311,9 +398,9 @@ var offset = i * reduceSize;

};
MathBackendCPU.prototype.argMax = function (input, axes) {
axis_util.assertAxesAreInnerMostDims('argMax', axes, input.rank);
var _a = axis_util.computeOutAndReduceShapes(input.shape, axes), outShape = _a[0], reduceShape = _a[1];
MathBackendCPU.prototype.argMax = function (x, axes) {
axis_util.assertAxesAreInnerMostDims('argMax', axes, x.rank);
var _a = axis_util.computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
var result = ndarray_1.NDArray.zeros(outShape, 'int32');
var reduceSize = util.sizeFromShape(reduceShape);
var vals = result.getValues();
var aVals = input.getValues();
var aVals = x.getValues();
for (var i = 0; i < vals.length; ++i) {

@@ -348,10 +435,10 @@ var offset = i * reduceSize;

};
MathBackendCPU.prototype.topKValues = function (ndarray, k) {
return this.topK(ndarray, k).values;
MathBackendCPU.prototype.topKValues = function (x, k) {
return this.topK(x, k).values;
};
MathBackendCPU.prototype.topKIndices = function (ndarray, k) {
return this.topK(ndarray, k).indices;
MathBackendCPU.prototype.topKIndices = function (x, k) {
return this.topK(x, k).indices;
};
MathBackendCPU.prototype.topK = function (ndarray, k) {
var values = ndarray.getValues();
MathBackendCPU.prototype.topK = function (x, k) {
var values = x.getValues();
var valuesAndIndices = [];

@@ -364,3 +451,3 @@ for (var i = 0; i < values.length; i++) {

});
var topkValues = util.getTypedArrayFromDType(ndarray.dtype, k);
var topkValues = util.getTypedArrayFromDType(x.dtype, k);
var topkIndices = new Int32Array(k);

@@ -376,9 +463,9 @@ for (var i = 0; i < k; i++) {

};
MathBackendCPU.prototype.min = function (input, axes) {
axis_util.assertAxesAreInnerMostDims('min', axes, input.rank);
var _a = axis_util.computeOutAndReduceShapes(input.shape, axes), outShape = _a[0], reduceShape = _a[1];
var result = ndarray_1.NDArray.zeros(outShape, input.dtype);
MathBackendCPU.prototype.min = function (x, axes) {
axis_util.assertAxesAreInnerMostDims('min', axes, x.rank);
var _a = axis_util.computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
var result = ndarray_1.NDArray.zeros(outShape, x.dtype);
var reduceSize = util.sizeFromShape(reduceShape);
var vals = result.getValues();
var aVals = input.getValues();
var aVals = x.getValues();
for (var i = 0; i < vals.length; ++i) {

@@ -401,9 +488,9 @@ var offset = i * reduceSize;

};
MathBackendCPU.prototype.max = function (input, axes) {
axis_util.assertAxesAreInnerMostDims('max', axes, input.rank);
var _a = axis_util.computeOutAndReduceShapes(input.shape, axes), outShape = _a[0], reduceShape = _a[1];
var result = ndarray_1.NDArray.zeros(outShape, input.dtype);
MathBackendCPU.prototype.max = function (x, axes) {
axis_util.assertAxesAreInnerMostDims('max', axes, x.rank);
var _a = axis_util.computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
var result = ndarray_1.NDArray.zeros(outShape, x.dtype);
var reduceSize = util.sizeFromShape(reduceShape);
var vals = result.getValues();
var aVals = input.getValues();
var aVals = x.getValues();
for (var i = 0; i < vals.length; ++i) {

@@ -426,4 +513,4 @@ var offset = i * reduceSize;

};
MathBackendCPU.prototype.ceil = function (ndarray) {
var values = ndarray.getValues();
MathBackendCPU.prototype.ceil = function (x) {
var values = x.getValues();
var newValues = new Float32Array(values.length);

@@ -433,6 +520,6 @@ for (var i = 0; i < values.length; ++i) {

}
return ndarray_1.NDArray.make(ndarray.shape, { values: newValues });
return ndarray_1.NDArray.make(x.shape, { values: newValues });
};
MathBackendCPU.prototype.floor = function (ndarray) {
var values = ndarray.getValues();
MathBackendCPU.prototype.floor = function (x) {
var values = x.getValues();
var newValues = new Float32Array(values.length);

@@ -442,6 +529,6 @@ for (var i = 0; i < values.length; ++i) {

}
return ndarray_1.NDArray.make(ndarray.shape, { values: newValues });
return ndarray_1.NDArray.make(x.shape, { values: newValues });
};
MathBackendCPU.prototype.exp = function (ndarray) {
var values = ndarray.getValues();
MathBackendCPU.prototype.exp = function (x) {
var values = x.getValues();
var newValues = new Float32Array(values.length);

@@ -451,6 +538,6 @@ for (var i = 0; i < values.length; ++i) {

}
return ndarray_1.NDArray.make(ndarray.shape, { values: newValues });
return ndarray_1.NDArray.make(x.shape, { values: newValues });
};
MathBackendCPU.prototype.log = function (ndarray) {
var values = ndarray.getValues();
MathBackendCPU.prototype.log = function (x) {
var values = x.getValues();
var newValues = new Float32Array(values.length);

@@ -461,6 +548,6 @@ for (var i = 0; i < values.length; ++i) {

}
return ndarray_1.NDArray.make(ndarray.shape, { values: newValues });
return ndarray_1.NDArray.make(x.shape, { values: newValues });
};
MathBackendCPU.prototype.sqrt = function (ndarray) {
var values = ndarray.getValues();
MathBackendCPU.prototype.sqrt = function (x) {
var values = x.getValues();
var newValues = new Float32Array(values.length);

@@ -471,3 +558,3 @@ for (var i = 0; i < values.length; ++i) {

}
return ndarray_1.NDArray.make(ndarray.shape, { values: newValues });
return ndarray_1.NDArray.make(x.shape, { values: newValues });
};

@@ -483,9 +570,9 @@ MathBackendCPU.prototype.square = function (x) {

};
MathBackendCPU.prototype.relu = function (input) {
var res = ndarray_1.NDArray.zeros(input.shape, input.dtype);
MathBackendCPU.prototype.relu = function (x) {
var res = ndarray_1.NDArray.zeros(x.shape, x.dtype);
var resVals = res.getValues();
var inVals = input.getValues();
var inVals = x.getValues();
for (var i = 0; i < inVals.length; ++i) {
var val = inVals[i];
if (util.isValNaN(val, input.dtype)) {
if (util.isValNaN(val, x.dtype)) {
resVals[i] = util.getNaN(res.dtype);

@@ -499,5 +586,5 @@ }

};
MathBackendCPU.prototype.elu = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.dataSync();
MathBackendCPU.prototype.elu = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.dataSync();
for (var i = 0; i < values.length; ++i) {

@@ -512,7 +599,7 @@ var v = values[i];

}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.eluDer = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.dataSync();
MathBackendCPU.prototype.eluDer = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.dataSync();
for (var i = 0; i < values.length; ++i) {

@@ -527,9 +614,9 @@ var v = values[i];

}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.selu = function (ndarray) {
MathBackendCPU.prototype.selu = function (x) {
var scaleAlpha = 1.7580993408473768599402175208123;
var scale = 1.0507009873554804934193349852946;
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.dataSync();
var resultValues = new Float32Array(x.size);
var values = x.dataSync();
for (var i = 0; i < values.length; ++i) {

@@ -544,7 +631,7 @@ var v = values[i];

}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.leakyRelu = function (ndarray, alpha) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.dataSync();
MathBackendCPU.prototype.leakyRelu = function (x, alpha) {
var resultValues = new Float32Array(x.size);
var values = x.dataSync();
for (var i = 0; i < values.length; i++) {

@@ -559,104 +646,104 @@ var v = values[i];

}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.clip = function (ndarray, min, max) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.clip = function (x, min, max) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = Math.min(max, Math.max(min, values[i]));
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.abs = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.abs = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = Math.abs(values[i]);
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.sigmoid = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.sigmoid = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = 1 / (1 + Math.exp(-values[i]));
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.sin = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.sin = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = Math.sin(values[i]);
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.cos = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.cos = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = Math.cos(values[i]);
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.tan = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.tan = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = Math.tan(values[i]);
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.asin = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.asin = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = Math.asin(values[i]);
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.acos = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.acos = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = Math.acos(values[i]);
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.atan = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.atan = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = Math.atan(values[i]);
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.sinh = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.sinh = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = Math.sinh(values[i]);
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.cosh = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.cosh = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = Math.cosh(values[i]);
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.tanh = function (ndarray) {
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
MathBackendCPU.prototype.tanh = function (x) {
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {
resultValues[i] = util.tanh(values[i]);
}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};
MathBackendCPU.prototype.step = function (ndarray, alpha) {
MathBackendCPU.prototype.step = function (x, alpha) {
if (alpha === void 0) { alpha = 0; }
var resultValues = new Float32Array(ndarray.size);
var values = ndarray.getValues();
var resultValues = new Float32Array(x.size);
var values = x.getValues();
for (var i = 0; i < values.length; ++i) {

@@ -666,3 +753,3 @@ var value = values[i];

}
return ndarray_1.NDArray.make(ndarray.shape, { values: resultValues });
return ndarray_1.NDArray.make(x.shape, { values: resultValues });
};

@@ -742,3 +829,3 @@ MathBackendCPU.prototype.conv2d = function (x, filter, bias, convInfo) {

};
MathBackendCPU.prototype.conv2dDerFilter = function (x, dY, convInfo) {
MathBackendCPU.prototype.conv2dDerFilter = function (x, dy, convInfo) {
var strideHeight = convInfo.strideHeight;

@@ -765,3 +852,3 @@ var strideWidth = convInfo.strideWidth;

var xC = wC + yC * strideWidth - leftPad;
dotProd += x.get(b, xR, xC, d1) * dY.get(b, yR, yC, d2);
dotProd += x.get(b, xR, xC, d1) * dy.get(b, yR, yC, d2);
}

@@ -777,4 +864,4 @@ }

};
MathBackendCPU.prototype.conv2dDerBias = function (dY) {
var _a = dY.shape, batchSize = _a[0], numRows = _a[1], numCols = _a[2], outDepth = _a[3];
MathBackendCPU.prototype.conv2dDerBias = function (dy) {
var _a = dy.shape, batchSize = _a[0], numRows = _a[1], numCols = _a[2], outDepth = _a[3];
var values = new Float32Array(outDepth);

@@ -786,3 +873,3 @@ for (var d2 = 0; d2 < outDepth; ++d2) {

for (var c = 0; c < numCols; ++c) {
sum += dY.get(b, r, c, d2);
sum += dy.get(b, r, c, d2);
}

@@ -795,3 +882,3 @@ }

};
MathBackendCPU.prototype.depthwiseConv2D = function (input, filter, convInfo) {
MathBackendCPU.prototype.depthwiseConv2D = function (x, filter, convInfo) {
var filterHeight = convInfo.filterHeight;

@@ -819,3 +906,3 @@ var filterWidth = convInfo.filterWidth;

var wC = xC - xCCorner;
var pixel = input.get(b, xR, xC, d1);
var pixel = x.get(b, xR, xC, d1);
var weight = filter.get(wR, wC, d1, q);

@@ -833,30 +920,30 @@ dotProd += pixel * weight;

};
MathBackendCPU.prototype.tile = function (a, reps) {
var newShape = new Array(a.rank);
MathBackendCPU.prototype.tile = function (x, reps) {
var newShape = new Array(x.rank);
for (var i = 0; i < newShape.length; i++) {
newShape[i] = a.shape[i] * reps[i];
newShape[i] = x.shape[i] * reps[i];
}
var dtype;
if (a.dtype === 'float32') {
if (x.dtype === 'float32') {
dtype = Float32Array;
}
else if (a.dtype === 'int32') {
else if (x.dtype === 'int32') {
dtype = Int32Array;
}
else if (a.dtype === 'bool') {
else if (x.dtype === 'bool') {
dtype = Uint8Array;
}
else {
throw new Error("Dtype " + a.dtype + " not supported for tile");
throw new Error("Dtype " + x.dtype + " not supported for tile");
}
var resultValues = new dtype(util.sizeFromShape(newShape));
var result = ndarray_1.NDArray.make(newShape, { values: resultValues }, a.dtype);
var values = a.getValues();
var result = ndarray_1.NDArray.make(newShape, { values: resultValues }, x.dtype);
var values = x.getValues();
for (var i = 0; i < result.size; ++i) {
var newLoc = result.indexToLoc(i);
var originalLoc = new Array(a.rank);
var originalLoc = new Array(x.rank);
for (var i_1 = 0; i_1 < originalLoc.length; i_1++) {
originalLoc[i_1] = newLoc[i_1] % a.shape[i_1];
originalLoc[i_1] = newLoc[i_1] % x.shape[i_1];
}
var originalIndex = a.locToIndex(originalLoc);
var originalIndex = x.locToIndex(originalLoc);
resultValues[i] = values[originalIndex];

@@ -866,12 +953,12 @@ }

};
MathBackendCPU.prototype.transpose = function (a, perm) {
var newShape = new Array(a.rank);
MathBackendCPU.prototype.transpose = function (x, perm) {
var newShape = new Array(x.rank);
for (var i = 0; i < newShape.length; i++) {
newShape[i] = a.shape[perm[i]];
newShape[i] = x.shape[perm[i]];
}
var resultValues = new Float32Array(a.size);
var values = a.getValues();
var resultValues = new Float32Array(x.size);
var values = x.getValues();
var result = ndarray_1.NDArray.make(newShape, { values: resultValues });
for (var i = 0; i < a.size; ++i) {
var loc = a.indexToLoc(i);
for (var i = 0; i < x.size; ++i) {
var loc = x.indexToLoc(i);
var newLoc = new Array(loc.length);

@@ -1149,2 +1236,3 @@ for (var i_2 = 0; i_2 < newLoc.length; i_2++) {

exports.MathBackendCPU = MathBackendCPU;
backend_1.BACKEND_REGISTRY['cpu'] = new MathBackendCPU();
var NDArrayMathCPU = (function (_super) {

@@ -1154,3 +1242,3 @@ __extends(NDArrayMathCPU, _super);

if (safeMode === void 0) { safeMode = false; }
return _super.call(this, new MathBackendCPU(), safeMode) || this;
return _super.call(this, 'cpu', safeMode) || this;
}

@@ -1157,0 +1245,0 @@ return NDArrayMathCPU;

import { Conv2DInfo } from '../conv_util';
import { NDArrayMath } from '../math';
import { Array1D, Array2D, Array3D, Array4D, DataTypes, NDArray, Scalar } from '../ndarray';
import { Array1D, Array2D, Array3D, Array4D, DataTypes, NDArray } from '../ndarray';
import { SumTypes } from '../types';
import { MathBackend, MatrixOrientation } from './backend';
import { MathBackend } from './backend';
import { MatrixOrientation } from './types/matmul';
import { GPGPUContext } from './webgl/gpgpu_context';
import { TextureManager } from './webgl/texture_manager';
export declare class MathBackendWebGL implements MathBackend {
private texData;
writePixels(id: number, pixels: ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement, numChannels: number): void;
write<T extends keyof DataTypes>(id: number, values: DataTypes[T], dtype: T, shape: number[]): void;
readSync<T extends keyof DataTypes>(id: number): DataTypes[T];
read<T extends keyof DataTypes>(id: number): Promise<DataTypes[T]>;
disposeData(id: number): void;
private gpgpu;

@@ -15,16 +22,13 @@ private textureManager;

getGPGPUContext(): GPGPUContext;
clone<G extends keyof DataTypes, T extends NDArray<G>>(a: T): T;
slice1D(input: Array1D, begin: number, size: number): Array1D;
slice2D(input: Array2D, begin: [number, number], size: [number, number]): Array2D;
slice3D(input: Array3D, begin: [number, number, number], size: [number, number, number]): Array3D;
slice4D(input: Array4D, begin: [number, number, number, number], size: [number, number, number, number]): Array4D;
copy2D(source: Array2D, sourceBeginRowCol: [number, number], sourceSizeRowCol: [number, number], dest: Array2D, destBeginRowCol: [number, number], destSizeRowCol: [number, number]): void;
clone<G extends keyof DataTypes, T extends NDArray<G>>(x: T): T;
slice1D(x: Array1D, begin: number, size: number): Array1D;
slice2D(x: Array2D, begin: [number, number], size: [number, number]): Array2D;
slice3D(x: Array3D, begin: [number, number, number], size: [number, number, number]): Array3D;
slice4D(x: Array4D, begin: [number, number, number, number], size: [number, number, number, number]): Array4D;
private copy2D(source, sourceBeginRowCol, sourceSizeRowCol, dest, destBeginRowCol, destSizeRowCol);
concat1D(a: Array1D, b: Array1D): Array1D;
concat2D(a: Array2D, b: Array2D, axis: number): Array2D;
concat3D(x1: Array3D, x2: Array3D, axis: number): Array3D;
concat4D(x1: Array4D, x2: Array4D, axis: number): Array4D;
scaledArrayAdd<T extends NDArray>(c1: Scalar, a: T, c2: Scalar, b: T): T;
neg<T extends NDArray>(a: T): T;
private makeOutputArray<G, T>(shape, dtype);
private compileAndRun<T, K>(program, inputs, output?, customSetup?);
concat3D(a: Array3D, b: Array3D, axis: number): Array3D;
concat4D(a: Array4D, b: Array4D, axis: number): Array4D;
neg<T extends NDArray>(x: T): T;
matMul(a: Array2D, b: Array2D, aOrientation: MatrixOrientation, bOrientation: MatrixOrientation): Array2D;

@@ -34,46 +38,47 @@ multiply<T extends NDArray>(a: T, b: T): T;

batchNormalization3D(x: Array3D, mean: Array3D | Array1D, variance: Array3D | Array1D, varianceEpsilon: number, scale?: Array3D | Array1D, offset?: Array3D | Array1D): Array3D;
tile<D extends keyof DataTypes, T extends NDArray<D>>(a: T, reps: number[]): T;
transpose<D extends keyof DataTypes, T extends NDArray<D>>(a: T, perm: number[]): T;
private reduce<D>(a, reduceType, dtype);
private argReduce(a, reduceType, bestIndicesA?);
sum<T extends keyof DataTypes>(a: NDArray<T>, axes: number[]): NDArray<SumTypes[T]>;
argMin(a: NDArray, axes: number[]): NDArray<'int32'>;
argMax(a: NDArray, axes: number[]): NDArray<'int32'>;
equal(x: NDArray, y: NDArray): NDArray<'bool'>;
topKValues<D extends keyof DataTypes, T extends NDArray<D>>(ndarray: T, k: number): Array1D<D>;
topKIndices(ndarray: NDArray, k: number): Array1D<'int32'>;
min<G extends keyof DataTypes>(a: NDArray<G>, axes: number[]): NDArray<G>;
max<G extends keyof DataTypes>(a: NDArray<G>, axes: number[]): NDArray<G>;
tile<D extends keyof DataTypes, T extends NDArray<D>>(x: T, reps: number[]): T;
transpose<D extends keyof DataTypes, T extends NDArray<D>>(x: T, perm: number[]): T;
private reduce<D>(x, reduceType, dtype);
private argReduce(x, reduceType, bestIndicesA?);
sum<T extends keyof DataTypes>(x: NDArray<T>, axes: number[]): NDArray<SumTypes[T]>;
argMin(x: NDArray, axes: number[]): NDArray<'int32'>;
argMax(x: NDArray, axes: number[]): NDArray<'int32'>;
equal(a: NDArray, b: NDArray): NDArray<'bool'>;
topKValues<D extends keyof DataTypes, T extends NDArray<D>>(x: T, k: number): Array1D<D>;
topKIndices(x: NDArray, k: number): Array1D<'int32'>;
min<G extends keyof DataTypes>(x: NDArray<G>, axes: number[]): NDArray<G>;
max<G extends keyof DataTypes>(x: NDArray<G>, axes: number[]): NDArray<G>;
divide(a: NDArray, b: NDArray): NDArray<'float32'>;
add<T extends NDArray>(a: T, b: T): T;
subtract<T extends NDArray>(a: T, b: T): T;
ceil<T extends NDArray>(a: T): T;
floor<T extends NDArray>(a: T): T;
exp<T extends NDArray>(a: T): T;
log<T extends NDArray>(a: T): T;
sqrt<T extends NDArray>(a: T): T;
pow<T extends NDArray>(a: T, b: NDArray<'int32'>): T;
ceil<T extends NDArray>(x: T): T;
floor<T extends NDArray>(x: T): T;
exp<T extends NDArray>(x: T): T;
log<T extends NDArray>(x: T): T;
sqrt<T extends NDArray>(x: T): T;
square<T extends NDArray>(x: T): T;
relu<T extends NDArray>(a: T): T;
elu<T extends NDArray>(a: T): T;
eluDer<T extends NDArray>(a: T): T;
selu<T extends NDArray>(a: T): T;
leakyRelu<T extends NDArray>(a: T, alpha: number): T;
clip<T extends NDArray>(a: T, min: number, max: number): T;
abs<T extends NDArray>(a: T): T;
sigmoid<T extends NDArray>(a: T): T;
sin<T extends NDArray>(a: T): T;
cos<T extends NDArray>(a: T): T;
tan<T extends NDArray>(a: T): T;
asin<T extends NDArray>(a: T): T;
acos<T extends NDArray>(a: T): T;
atan<T extends NDArray>(a: T): T;
sinh<T extends NDArray>(a: T): T;
cosh<T extends NDArray>(a: T): T;
tanh<T extends NDArray>(a: T): T;
step<T extends NDArray>(a: T, alpha: number): T;
relu<T extends NDArray>(x: T): T;
elu<T extends NDArray>(x: T): T;
eluDer<T extends NDArray>(x: T): T;
selu<T extends NDArray>(x: T): T;
leakyRelu<T extends NDArray>(x: T, alpha: number): T;
clip<T extends NDArray>(x: T, min: number, max: number): T;
abs<T extends NDArray>(x: T): T;
sigmoid<T extends NDArray>(x: T): T;
sin<T extends NDArray>(x: T): T;
cos<T extends NDArray>(x: T): T;
tan<T extends NDArray>(x: T): T;
asin<T extends NDArray>(x: T): T;
acos<T extends NDArray>(x: T): T;
atan<T extends NDArray>(x: T): T;
sinh<T extends NDArray>(x: T): T;
cosh<T extends NDArray>(x: T): T;
tanh<T extends NDArray>(x: T): T;
step<T extends NDArray>(x: T, alpha: number): T;
conv2d(x: Array4D, filter: Array4D, bias: Array1D | null, convInfo: Conv2DInfo): Array4D;
conv2dDerInput(dy: Array4D, filter: Array4D, convInfo: Conv2DInfo): Array4D;
conv2dDerFilter(x: Array4D, dY: Array4D, convInfo: Conv2DInfo): Array4D;
conv2dDerBias(dY: Array4D): Array1D;
depthwiseConv2D(input: Array4D, filter: Array4D, convInfo: Conv2DInfo): Array4D;
conv2dDerFilter(x: Array4D, dy: Array4D, convInfo: Conv2DInfo): Array4D;
conv2dDerBias(dy: Array4D): Array1D;
depthwiseConv2D(x: Array4D, filter: Array4D, convInfo: Conv2DInfo): Array4D;
maxPool(x: Array4D, convInfo: Conv2DInfo): Array4D;

@@ -86,2 +91,4 @@ minPool(x: Array4D, convInfo: Conv2DInfo): Array4D;

oneHot(indices: Array1D, depth: number, onValue: number, offValue: number): Array2D;
private makeOutputArray<G, T>(shape, dtype);
private compileAndRun<T, K>(program, inputs, output?, customSetup?);
private getAndSaveBinary(key, getBinary);

@@ -88,0 +95,0 @@ getTextureManager(): TextureManager;

@@ -12,11 +12,46 @@ "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 = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var environment_1 = require("../../environment");
var util = require("../../util");
var axis_util = require("../axis_util");
var math_1 = require("../math");
var ndarray = require("../ndarray");
var ndarray_1 = require("../ndarray");
var reduce_util = require("../reduce_util");
var types_1 = require("../types");
var addscaledmat_gpu_1 = require("./webgl/addscaledmat_gpu");
var backend_1 = require("./backend");
var argminmax_gpu_1 = require("./webgl/argminmax_gpu");

@@ -43,2 +78,3 @@ var batchnorm_gpu_1 = require("./webgl/batchnorm_gpu");

var slice_gpu_1 = require("./webgl/slice_gpu");
var tex_util_1 = require("./webgl/tex_util");
var texture_manager_1 = require("./webgl/texture_manager");

@@ -52,2 +88,3 @@ var tile_gpu_1 = require("./webgl/tile_gpu");

function MathBackendWebGL(gpgpu) {
this.texData = {};
this.binaryCache = {};

@@ -64,33 +101,96 @@ if (gpgpu == null) {

this.textureManager = new texture_manager_1.TextureManager(this.gpgpu);
ndarray.initializeGPU(this.gpgpu, this.textureManager);
}
MathBackendWebGL.prototype.writePixels = function (id, pixels, numChannels) {
var shape = [pixels.height, pixels.width, numChannels];
var texShape = [shape[0], shape[1]];
var texture = this.textureManager.acquireTexture(texShape);
this.gpgpu.uploadPixelDataToTexture(texture, pixels);
this.texData[id] = {
texture: texture,
textureType: tex_util_1.TextureType.RGBA_COLOR,
texShape: texShape,
numChannels: numChannels,
dtype: 'int32'
};
};
MathBackendWebGL.prototype.write = function (id, values, dtype, shape) {
var texShape = webgl_util.getTextureShapeFromLogicalShape(this.gpgpu.gl, shape);
var texture = this.textureManager.acquireTexture(texShape);
var textureType = tex_util_1.TextureType.DEFAULT;
this.texData[id] = { texture: texture, textureType: textureType, texShape: texShape, dtype: dtype };
if (values != null) {
this.gpgpu.uploadMatrixToTexture(texture, texShape[0], texShape[1], typedArrayToFloat32(values, dtype));
}
};
MathBackendWebGL.prototype.readSync = function (id) {
var values;
var _a = this.texData[id], texture = _a.texture, textureType = _a.textureType, texShape = _a.texShape, numChannels = _a.numChannels, dtype = _a.dtype;
if (textureType === tex_util_1.TextureType.DEFAULT) {
values = this.gpgpu.downloadMatrixFromTexture(texture, texShape[0], texShape[1]);
}
else {
values = this.gpgpu.downloadMatrixFromRGBAColorTexture(texture, texShape[0], texShape[1], numChannels);
}
return float32ToTypedArray(values, dtype);
};
MathBackendWebGL.prototype.read = function (id) {
return __awaiter(this, void 0, void 0, function () {
var _a, texture, textureType, texShape, queryFn;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this.texData[id], texture = _a.texture, textureType = _a.textureType, texShape = _a.texShape;
if (environment_1.ENV.get('WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED') &&
textureType === tex_util_1.TextureType.DEFAULT) {
return [2, this.gpgpu.downloadMatrixFromTextureAsync(texture, texShape[0], texShape[1])];
}
if (!!environment_1.ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_ENABLED')) return [3, 2];
return [4, this.readSync(id)];
case 1: return [2, _b.sent()];
case 2:
queryFn = function () { };
return [4, this.gpgpu.runQuery(queryFn)];
case 3:
_b.sent();
return [2, this.readSync(id)];
}
});
});
};
MathBackendWebGL.prototype.disposeData = function (id) {
if (id in this.texData) {
var _a = this.texData[id], texture = _a.texture, texShape = _a.texShape;
this.textureManager.releaseTexture(texture, texShape);
delete this.texData[id];
}
};
MathBackendWebGL.prototype.getGPGPUContext = function () {
return this.gpgpu;
};
MathBackendWebGL.prototype.clone = function (a) {
var texShape = a.getTextureShapeRC();
var source = a.as2D(texShape[0], texShape[1]);
var output = this.makeOutputArray(texShape, a.dtype);
MathBackendWebGL.prototype.clone = function (x) {
var texShape = this.texData[x.id].texShape;
var source = x.as2D(texShape[0], texShape[1]);
var output = this.makeOutputArray(texShape, x.dtype);
this.copy2D(source, [0, 0], texShape, output, [0, 0], texShape);
return output.reshape(a.shape);
return output.reshape(x.shape);
};
MathBackendWebGL.prototype.slice1D = function (input, begin, size) {
MathBackendWebGL.prototype.slice1D = function (x, begin, size) {
var program = new slice_gpu_1.SliceProgram([size]);
var customSetup = program.getCustomSetupFunc([begin]);
return this.compileAndRun(program, [input], null, customSetup);
return this.compileAndRun(program, [x], null, customSetup);
};
MathBackendWebGL.prototype.slice2D = function (input, begin, size) {
MathBackendWebGL.prototype.slice2D = function (x, begin, size) {
var program = new slice_gpu_1.SliceProgram(size);
var customSetup = program.getCustomSetupFunc(begin);
return this.compileAndRun(program, [input], null, customSetup);
return this.compileAndRun(program, [x], null, customSetup);
};
MathBackendWebGL.prototype.slice3D = function (input, begin, size) {
MathBackendWebGL.prototype.slice3D = function (x, begin, size) {
var program = new slice_gpu_1.SliceProgram(size);
var customSetup = program.getCustomSetupFunc(begin);
return this.compileAndRun(program, [input], null, customSetup);
return this.compileAndRun(program, [x], null, customSetup);
};
MathBackendWebGL.prototype.slice4D = function (input, begin, size) {
MathBackendWebGL.prototype.slice4D = function (x, begin, size) {
var program = new slice_gpu_1.SliceProgram(size);
var customSetup = program.getCustomSetupFunc(begin);
return this.compileAndRun(program, [input], null, customSetup);
return this.compileAndRun(program, [x], null, customSetup);
};

@@ -110,35 +210,14 @@ MathBackendWebGL.prototype.copy2D = function (source, sourceBeginRowCol, sourceSizeRowCol, dest, destBeginRowCol, destSizeRowCol) {

};
MathBackendWebGL.prototype.concat3D = function (x1, x2, axis) {
var program = new concat_gpu_1.ConcatProgram(x1.shape, x2.shape, axis);
return this.compileAndRun(program, [x1, x2]);
MathBackendWebGL.prototype.concat3D = function (a, b, axis) {
var program = new concat_gpu_1.ConcatProgram(a.shape, b.shape, axis);
return this.compileAndRun(program, [a, b]);
};
MathBackendWebGL.prototype.concat4D = function (x1, x2, axis) {
var program = new concat_gpu_1.ConcatProgram(x1.shape, x2.shape, axis);
return this.compileAndRun(program, [x1, x2]);
MathBackendWebGL.prototype.concat4D = function (a, b, axis) {
var program = new concat_gpu_1.ConcatProgram(a.shape, b.shape, axis);
return this.compileAndRun(program, [a, b]);
};
MathBackendWebGL.prototype.scaledArrayAdd = function (c1, a, c2, b) {
var program = new addscaledmat_gpu_1.AddScaledMatProgram(a.shape, b.shape);
return this.compileAndRun(program, [a, b, c1, c2]);
MathBackendWebGL.prototype.neg = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.NEG);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.neg = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.NEG);
return this.compileAndRun(program, [a]);
};
MathBackendWebGL.prototype.makeOutputArray = function (shape, dtype) {
var textureShapeRC = webgl_util.getTextureShapeFromLogicalShape(this.gpgpu.gl, shape);
var texture = this.textureManager.acquireTexture(textureShapeRC);
return ndarray_1.NDArray.make(shape, { texture: texture, textureShapeRC: textureShapeRC }, dtype);
};
MathBackendWebGL.prototype.compileAndRun = function (program, inputs, output, customSetup) {
var _this = this;
if (output == null) {
output = this.makeOutputArray(program.outputShape, inputs[0].dtype);
}
var key = gpgpu_math.makeShaderKey(program, inputs, output);
var binary = this.getAndSaveBinary(key, function () {
return gpgpu_math.compileProgram(_this.gpgpu, program, inputs, output);
});
gpgpu_math.runProgram(binary, inputs, output, customSetup);
return output;
};
MathBackendWebGL.prototype.matMul = function (a, b, aOrientation, bOrientation) {

@@ -182,13 +261,13 @@ var program = new mulmat_gpu_1.MatMulProgram(a.shape, b.shape, aOrientation, bOrientation);

};
MathBackendWebGL.prototype.tile = function (a, reps) {
var program = new tile_gpu_1.TileProgram(a.shape, reps);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.tile = function (x, reps) {
var program = new tile_gpu_1.TileProgram(x.shape, reps);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.transpose = function (a, perm) {
var program = new transpose_gpu_1.TransposeProgram(a.shape, perm);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.transpose = function (x, perm) {
var program = new transpose_gpu_1.TransposeProgram(x.shape, perm);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.reduce = function (a, reduceType, dtype) {
var batchSize = a.shape[0];
var inSize = a.shape[1];
MathBackendWebGL.prototype.reduce = function (x, reduceType, dtype) {
var batchSize = x.shape[0];
var inSize = x.shape[1];
var windowSize = reduce_util.computeOptimalWindowSize(inSize);

@@ -199,3 +278,3 @@ var reduceInfo = { windowSize: windowSize, inSize: inSize, batchSize: batchSize };

var output = this.makeOutputArray(program.outputShape, dtype).as2D(rows, cols);
this.compileAndRun(program, [a], output);
this.compileAndRun(program, [x], output);
if (output.shape[1] === 1) {

@@ -206,6 +285,6 @@ return output;

};
MathBackendWebGL.prototype.argReduce = function (a, reduceType, bestIndicesA) {
MathBackendWebGL.prototype.argReduce = function (x, reduceType, bestIndicesA) {
if (bestIndicesA === void 0) { bestIndicesA = null; }
var batchSize = a.shape[0];
var inSize = a.shape[1];
var batchSize = x.shape[0];
var inSize = x.shape[1];
if (bestIndicesA != null) {

@@ -220,3 +299,3 @@ batchSize = bestIndicesA.shape[0];

var output = this.makeOutputArray(program.outputShape, 'int32').as2D(rows, cols);
var inputs = [a];
var inputs = [x];
if (bestIndicesA != null) {

@@ -229,49 +308,49 @@ inputs.push(bestIndicesA);

}
return this.argReduce(a, reduceType, output);
return this.argReduce(x, reduceType, output);
};
MathBackendWebGL.prototype.sum = function (a, axes) {
axis_util.assertAxesAreInnerMostDims('sum', axes, a.rank);
var _a = axis_util.computeOutAndReduceShapes(a.shape, axes), outShape = _a[0], reduceShape = _a[1];
MathBackendWebGL.prototype.sum = function (x, axes) {
axis_util.assertAxesAreInnerMostDims('sum', axes, x.rank);
var _a = axis_util.computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
var inSize = util.sizeFromShape(reduceShape);
var a2D = a.as2D(-1, inSize);
var outputDType = types_1.SumTypesMap[a.dtype];
var a2D = x.as2D(-1, inSize);
var outputDType = types_1.SumTypesMap[x.dtype];
return this.reduce(a2D, 'sum', outputDType).reshape(outShape);
};
MathBackendWebGL.prototype.argMin = function (a, axes) {
axis_util.assertAxesAreInnerMostDims('argMin', axes, a.rank);
var _a = axis_util.computeOutAndReduceShapes(a.shape, axes), outShape = _a[0], reduceShape = _a[1];
MathBackendWebGL.prototype.argMin = function (x, axes) {
axis_util.assertAxesAreInnerMostDims('argMin', axes, x.rank);
var _a = axis_util.computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
var inSize = util.sizeFromShape(reduceShape);
var a2D = a.as2D(-1, inSize);
var a2D = x.as2D(-1, inSize);
return this.argReduce(a2D, 'min').reshape(outShape);
};
MathBackendWebGL.prototype.argMax = function (a, axes) {
axis_util.assertAxesAreInnerMostDims('argMax', axes, a.rank);
var _a = axis_util.computeOutAndReduceShapes(a.shape, axes), outShape = _a[0], reduceShape = _a[1];
MathBackendWebGL.prototype.argMax = function (x, axes) {
axis_util.assertAxesAreInnerMostDims('argMax', axes, x.rank);
var _a = axis_util.computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
var inSize = util.sizeFromShape(reduceShape);
var a2D = a.as2D(-1, inSize);
var a2D = x.as2D(-1, inSize);
return this.argReduce(a2D, 'max').reshape(outShape);
};
MathBackendWebGL.prototype.equal = function (x, y) {
var program = new binaryop_gpu_1.BinaryOpProgram(binaryop_gpu.EQUAL, x.shape, y.shape);
MathBackendWebGL.prototype.equal = function (a, b) {
var program = new binaryop_gpu_1.BinaryOpProgram(binaryop_gpu.EQUAL, a.shape, b.shape);
var output = this.makeOutputArray(program.outputShape, 'bool');
return this.compileAndRun(program, [x, y], output);
return this.compileAndRun(program, [a, b], output);
};
MathBackendWebGL.prototype.topKValues = function (ndarray, k) {
MathBackendWebGL.prototype.topKValues = function (x, k) {
throw new Error('topKValues GPU not yet implemented!');
};
MathBackendWebGL.prototype.topKIndices = function (ndarray, k) {
MathBackendWebGL.prototype.topKIndices = function (x, k) {
throw new Error('topKIndices GPU not yet implemented!');
};
MathBackendWebGL.prototype.min = function (a, axes) {
axis_util.assertAxesAreInnerMostDims('min', axes, a.rank);
var _a = axis_util.computeOutAndReduceShapes(a.shape, axes), outShape = _a[0], reduceShape = _a[1];
MathBackendWebGL.prototype.min = function (x, axes) {
axis_util.assertAxesAreInnerMostDims('min', axes, x.rank);
var _a = axis_util.computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
var inSize = util.sizeFromShape(reduceShape);
var a2D = a.as2D(-1, inSize);
var a2D = x.as2D(-1, inSize);
return this.reduce(a2D, 'min', a2D.dtype).reshape(outShape);
};
MathBackendWebGL.prototype.max = function (a, axes) {
axis_util.assertAxesAreInnerMostDims('max', axes, a.rank);
var _a = axis_util.computeOutAndReduceShapes(a.shape, axes), outShape = _a[0], reduceShape = _a[1];
MathBackendWebGL.prototype.max = function (x, axes) {
axis_util.assertAxesAreInnerMostDims('max', axes, x.rank);
var _a = axis_util.computeOutAndReduceShapes(x.shape, axes), outShape = _a[0], reduceShape = _a[1];
var inSize = util.sizeFromShape(reduceShape);
var a2D = a.as2D(-1, inSize);
var a2D = x.as2D(-1, inSize);
return this.reduce(a2D, 'max', a2D.dtype).reshape(outShape);

@@ -292,22 +371,26 @@ };

};
MathBackendWebGL.prototype.ceil = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.CEIL);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.pow = function (a, b) {
var program = new binaryop_gpu_1.BinaryOpProgram(binaryop_gpu.POW, a.shape, b.shape);
return this.compileAndRun(program, [a, b]);
};
MathBackendWebGL.prototype.floor = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.FLOOR);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.ceil = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.CEIL);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.exp = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.EXP);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.floor = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.FLOOR);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.log = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.LOG);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.exp = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.EXP);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.sqrt = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.SQRT);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.log = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.LOG);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.sqrt = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.SQRT);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.square = function (x) {

@@ -317,73 +400,73 @@ var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.SQUARE);

};
MathBackendWebGL.prototype.relu = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.RELU);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.relu = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.RELU);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.elu = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.ELU);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.elu = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.ELU);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.eluDer = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.ELU_DER);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.eluDer = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.ELU_DER);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.selu = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.SELU);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.selu = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.SELU);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.leakyRelu = function (a, alpha) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.LEAKY_RELU(alpha));
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.leakyRelu = function (x, alpha) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.LEAKY_RELU(alpha));
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.clip = function (a, min, max) {
var program = new clip_gpu_1.ClipProgram(a.shape, min, max);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.clip = function (x, min, max) {
var program = new clip_gpu_1.ClipProgram(x.shape, min, max);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.abs = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.ABS);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.abs = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.ABS);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.sigmoid = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.SIGMOID);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.sigmoid = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.SIGMOID);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.sin = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.SIN);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.sin = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.SIN);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.cos = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.COS);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.cos = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.COS);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.tan = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.TAN);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.tan = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.TAN);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.asin = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.ASIN);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.asin = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.ASIN);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.acos = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.ACOS);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.acos = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.ACOS);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.atan = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.ATAN);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.atan = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.ATAN);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.sinh = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.SINH);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.sinh = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.SINH);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.cosh = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.COSH);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.cosh = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.COSH);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.tanh = function (a) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.TANH);
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.tanh = function (x) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.TANH);
return this.compileAndRun(program, [x]);
};
MathBackendWebGL.prototype.step = function (a, alpha) {
var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.STEP(alpha));
return this.compileAndRun(program, [a]);
MathBackendWebGL.prototype.step = function (x, alpha) {
var program = new unaryop_gpu_1.UnaryOpProgram(x.shape, unary_op.STEP(alpha));
return this.compileAndRun(program, [x]);
};

@@ -399,13 +482,13 @@ MathBackendWebGL.prototype.conv2d = function (x, filter, bias, convInfo) {

};
MathBackendWebGL.prototype.conv2dDerFilter = function (x, dY, convInfo) {
MathBackendWebGL.prototype.conv2dDerFilter = function (x, dy, convInfo) {
var program = new conv_backprop_gpu_1.Conv2DDerFilterProgram(convInfo);
return this.compileAndRun(program, [x, dY]);
return this.compileAndRun(program, [x, dy]);
};
MathBackendWebGL.prototype.conv2dDerBias = function (dY) {
var program = new conv_backprop_gpu_1.Conv2DDerBiasProgram(dY.shape);
return this.compileAndRun(program, [dY]);
MathBackendWebGL.prototype.conv2dDerBias = function (dy) {
var program = new conv_backprop_gpu_1.Conv2DDerBiasProgram(dy.shape);
return this.compileAndRun(program, [dy]);
};
MathBackendWebGL.prototype.depthwiseConv2D = function (input, filter, convInfo) {
MathBackendWebGL.prototype.depthwiseConv2D = function (x, filter, convInfo) {
var program = new conv_gpu_depthwise_1.DepthwiseConv2DProgram(convInfo);
return this.compileAndRun(program, [input, filter]);
return this.compileAndRun(program, [x, filter]);
};

@@ -449,2 +532,21 @@ MathBackendWebGL.prototype.maxPool = function (x, convInfo) {

};
MathBackendWebGL.prototype.makeOutputArray = function (shape, dtype) {
return ndarray_1.NDArray.make(shape, {}, dtype);
};
MathBackendWebGL.prototype.compileAndRun = function (program, inputs, output, customSetup) {
var _this = this;
if (output == null) {
output = this.makeOutputArray(program.outputShape, inputs[0].dtype);
}
var inputsData = inputs.map(function (input) {
return { array: input, texData: _this.texData[input.id] };
});
var outputData = { array: output, texData: this.texData[output.id] };
var key = gpgpu_math.makeShaderKey(program, inputsData, outputData);
var binary = this.getAndSaveBinary(key, function () {
return gpgpu_math.compileProgram(_this.gpgpu, program, inputsData, outputData);
});
gpgpu_math.runProgram(binary, inputsData, outputData, customSetup);
return output;
};
MathBackendWebGL.prototype.getAndSaveBinary = function (key, getBinary) {

@@ -471,2 +573,3 @@ if (!(key in this.binaryCache)) {

exports.MathBackendWebGL = MathBackendWebGL;
backend_1.BACKEND_REGISTRY['webgl'] = new MathBackendWebGL();
var NDArrayMathGPU = (function (_super) {

@@ -479,6 +582,8 @@ __extends(NDArrayMathGPU, _super);

NDArrayMathGPU.prototype.getGPGPUContext = function () {
return this.backend.getGPGPUContext();
return this.backendEngine.getBackend()
.getGPGPUContext();
};
NDArrayMathGPU.prototype.getTextureManager = function () {
return this.backend.getTextureManager();
return this.backendEngine.getBackend()
.getTextureManager();
};

@@ -488,1 +593,32 @@ return NDArrayMathGPU;

exports.NDArrayMathGPU = NDArrayMathGPU;
function float32ToTypedArray(a, dtype) {
if (dtype === 'float32') {
return a;
}
else if (dtype === 'int32' || dtype === 'bool') {
var result = (dtype === 'int32') ? new Int32Array(a.length) :
new Uint8Array(a.length);
for (var i = 0; i < result.length; ++i) {
var val = a[i];
val = isNaN(val) ? util.getNaN(dtype) : Math.round(val);
result[i] = val;
}
return result;
}
else {
throw new Error("Unknown dtype " + dtype);
}
}
function typedArrayToFloat32(a, dtype) {
if (a instanceof Float32Array) {
return a;
}
else {
var res = new Float32Array(a.length);
for (var i = 0; i < res.length; i++) {
var val = a[i];
res[i] = util.isValNaN(val, dtype) ? NaN : val;
}
return res;
}
}

@@ -0,16 +1,23 @@

import { Backends } from '../../environment';
import { Conv2DInfo } from '../conv_util';
import { Array1D, Array2D, Array3D, Array4D, DataTypes, NDArray, Scalar } from '../ndarray';
import { Array1D, Array2D, Array3D, Array4D, DataTypes, NDArray } from '../ndarray';
import { SumTypes } from '../types';
export declare enum MatrixOrientation {
REGULAR = 0,
TRANSPOSED = 1,
import { MatrixOrientation } from './types/matmul';
export declare const BACKEND_REGISTRY: {
[id in Backends]: MathBackend;
};
export interface NDArrayStorage {
read<T extends keyof DataTypes>(id: number): Promise<DataTypes[T]>;
readSync<T extends keyof DataTypes>(id: number): DataTypes[T];
disposeData(id: number): void;
write<T extends keyof DataTypes>(id: number, values: DataTypes[T], dtype: T, shape: number[]): void;
writePixels(id: number, pixels: ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement, numChannels: number): void;
}
export interface MathBackend {
export interface MathBackend extends NDArrayStorage {
matMul(a: Array2D, b: Array2D, aOrientation: MatrixOrientation, bOrientation: MatrixOrientation): Array2D;
clone<T extends NDArray>(ndarray: T): T;
slice1D(input: Array1D, begin: number, size: number): Array1D;
slice2D(input: Array2D, begin: [number, number], size: [number, number]): Array2D;
slice3D(input: Array3D, begin: [number, number, number], size: [number, number, number]): Array3D;
slice4D(input: Array4D, begin: [number, number, number, number], size: [number, number, number, number]): Array4D;
copy2D(source: Array2D, sourceBeginRowCol: [number, number], sourceSizeRowCol: [number, number], dest: Array2D, destBeginRowCol: [number, number], destSizeRowCol: [number, number]): void;
slice1D(x: Array1D, begin: number, size: number): Array1D;
slice2D(x: Array2D, begin: [number, number], size: [number, number]): Array2D;
slice3D(x: Array3D, begin: [number, number, number], size: [number, number, number]): Array3D;
slice4D(x: Array4D, begin: [number, number, number, number], size: [number, number, number, number]): Array4D;
concat1D(a: Array1D, b: Array1D): Array1D;

@@ -20,40 +27,40 @@ concat2D(a: Array2D, b: Array2D, axis: number): Array2D;

concat4D(a: Array4D, b: Array4D, axis: number): Array4D;
scaledArrayAdd<T extends NDArray>(c1: Scalar, a: T, c2: Scalar, b: T): T;
neg<T extends NDArray>(a: T): T;
add<T extends NDArray>(a: T, b: T): T;
subtract<T extends NDArray>(a: T, b: T): T;
multiply<T extends NDArray>(a: T, b: T): T;
divide(a: NDArray, b: NDArray): NDArray<'float32'>;
sum<T extends keyof DataTypes>(input: NDArray<T>, axes: number[]): NDArray<SumTypes[T]>;
argMin(input: NDArray, axes: number[]): NDArray<'int32'>;
argMax(input: NDArray, axes: number[]): NDArray<'int32'>;
add<G extends keyof DataTypes>(a: NDArray<G>, b: NDArray<G>): NDArray<G>;
subtract<G extends keyof DataTypes>(a: NDArray<G>, b: NDArray<G>): NDArray<G>;
multiply<G extends keyof DataTypes>(a: NDArray<G>, b: NDArray<G>): NDArray<G>;
divide<G extends keyof DataTypes>(a: NDArray<G>, b: NDArray<G>): NDArray<'float32'>;
sum<T extends keyof DataTypes>(x: NDArray<T>, axes: number[]): NDArray<SumTypes[T]>;
argMin(x: NDArray, axes: number[]): NDArray<'int32'>;
argMax(x: NDArray, axes: number[]): NDArray<'int32'>;
equal(a: NDArray, b: NDArray): NDArray<'bool'>;
topKValues<D extends keyof DataTypes, T extends NDArray<D>>(ndarray: T, k: number): Array1D<D>;
topKIndices(ndarray: NDArray, k: number): Array1D<'int32'>;
min<G extends keyof DataTypes>(input: NDArray<G>, axes: number[]): NDArray<G>;
max<G extends keyof DataTypes>(input: NDArray<G>, axes: number[]): NDArray<G>;
ceil<T extends NDArray>(ndarray: T): T;
floor<T extends NDArray>(ndarray: T): T;
exp<T extends NDArray>(ndarray: T): T;
log<T extends NDArray>(ndarray: T): T;
sqrt<T extends NDArray>(ndarray: T): T;
topKValues<D extends keyof DataTypes, T extends NDArray<D>>(x: T, k: number): Array1D<D>;
topKIndices(x: NDArray, k: number): Array1D<'int32'>;
min<G extends keyof DataTypes>(x: NDArray<G>, axes: number[]): NDArray<G>;
max<G extends keyof DataTypes>(x: NDArray<G>, axes: number[]): NDArray<G>;
ceil<T extends NDArray>(x: T): T;
floor<T extends NDArray>(x: T): T;
pow<T extends NDArray>(a: T, b: NDArray<'int32'>): T;
exp<T extends NDArray>(x: T): T;
log<T extends NDArray>(x: T): T;
sqrt<T extends NDArray>(x: T): T;
square<T extends NDArray>(x: T): T;
relu<T extends NDArray>(input: T): T;
elu<T extends NDArray>(ndarray: T): T;
eluDer<T extends NDArray>(ndarray: T): T;
selu<T extends NDArray>(a: T): T;
leakyRelu<T extends NDArray>(ndarray: T, alpha: number): T;
clip<T extends NDArray>(ndarray: T, min: number, max: number): T;
abs<T extends NDArray>(ndarray: T): T;
sigmoid<T extends NDArray>(ndarray: T): T;
sin<T extends NDArray>(ndarray: T): T;
cos<T extends NDArray>(ndarray: T): T;
tan<T extends NDArray>(ndarray: T): T;
asin<T extends NDArray>(ndarray: T): T;
acos<T extends NDArray>(ndarray: T): T;
atan<T extends NDArray>(ndarray: T): T;
sinh<T extends NDArray>(ndarray: T): T;
cosh<T extends NDArray>(ndarray: T): T;
tanh<T extends NDArray>(ndarray: T): T;
step<T extends NDArray>(ndarray: T, alpha: number): T;
relu<T extends NDArray>(x: T): T;
elu<T extends NDArray>(x: T): T;
eluDer<T extends NDArray>(x: T): T;
selu<T extends NDArray>(x: T): T;
leakyRelu<T extends NDArray>(x: T, alpha: number): T;
clip<T extends NDArray>(x: T, min: number, max: number): T;
abs<T extends NDArray>(x: T): T;
sigmoid<T extends NDArray>(x: T): T;
sin<T extends NDArray>(x: T): T;
cos<T extends NDArray>(x: T): T;
tan<T extends NDArray>(x: T): T;
asin<T extends NDArray>(x: T): T;
acos<T extends NDArray>(x: T): T;
atan<T extends NDArray>(x: T): T;
sinh<T extends NDArray>(x: T): T;
cosh<T extends NDArray>(x: T): T;
tanh<T extends NDArray>(x: T): T;
step<T extends NDArray>(x: T, alpha: number): T;
conv2d(x: Array4D, filter: Array4D, bias: Array1D | null, convInfo: Conv2DInfo): Array4D;

@@ -68,4 +75,4 @@ conv2dDerInput(dy: Array4D, filter: Array4D, convInfo: Conv2DInfo): Array4D;

avgPool(x: Array4D, convInfo: Conv2DInfo): Array4D;
tile<D extends keyof DataTypes, T extends NDArray<D>>(a: T, reps: number[]): T;
transpose<D extends keyof DataTypes, T extends NDArray<D>>(a: T, perm: number[]): T;
tile<D extends keyof DataTypes, T extends NDArray<D>>(x: T, reps: number[]): T;
transpose<D extends keyof DataTypes, T extends NDArray<D>>(x: T, perm: number[]): T;
resizeBilinear3D(x: Array3D, newShape2D: [number, number], alignCorners: boolean): Array3D;

@@ -76,2 +83,3 @@ batchNormalization2D(x: Array2D, mean: Array2D | Array1D, variance: Array2D | Array1D, varianceEpsilon: number, scale?: Array2D | Array1D, offset?: Array2D | Array1D): Array2D;

oneHot(indices: Array1D, depth: number, onValue: number, offValue: number): Array2D;
dispose(): void;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MatrixOrientation;
(function (MatrixOrientation) {
MatrixOrientation[MatrixOrientation["REGULAR"] = 0] = "REGULAR";
MatrixOrientation[MatrixOrientation["TRANSPOSED"] = 1] = "TRANSPOSED";
})(MatrixOrientation = exports.MatrixOrientation || (exports.MatrixOrientation = {}));
exports.BACKEND_REGISTRY = {};

@@ -6,2 +6,3 @@ import { GPGPUProgram } from './gpgpu_math';

export declare const DIV = "return a / b;";
export declare const POW = "\n return (round(mod(b, 2.0)) == 0 || round(mod(b, 2.0)) == 2) ?\n pow(abs(a), b) : sign(a) * pow(abs(a), b);\n";
export declare const EQUAL = "\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n return float(a == b);\n";

@@ -8,0 +9,0 @@ export declare class BinaryOpProgram implements GPGPUProgram {

@@ -8,2 +8,3 @@ "use strict";

exports.DIV = 'return a / b;';
exports.POW = "\n return (round(mod(b, 2.0)) == 0 || round(mod(b, 2.0)) == 2) ?\n pow(abs(a), b) : sign(a) * pow(abs(a), b);\n";
exports.EQUAL = "\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n return float(a == b);\n";

@@ -10,0 +11,0 @@ var BinaryOpProgram = (function () {

import { NDArray } from '../../ndarray';
import { GPGPUContext } from './gpgpu_context';
import { ShapeInfo } from './shader_compiler';
import { TextureData } from './tex_util';
export interface GPGPUProgram {

@@ -24,4 +25,8 @@ variableNames: string[];

}
export declare function compileProgram<T extends NDArray, K extends NDArray>(gpgpu: GPGPUContext, program: GPGPUProgram, inputs: T[], output: K): GPGPUBinary;
export declare function runProgram<T extends NDArray, K extends NDArray>(binary: GPGPUBinary, inputs: T[], output: K, customSetup?: (gpgpu: GPGPUContext, webGLProgram: WebGLProgram) => void): void;
export declare function makeShaderKey(program: GPGPUProgram, inputs: NDArray[], output: NDArray): string;
export interface ArrayData<T extends NDArray> {
array: T;
texData: TextureData;
}
export declare function compileProgram<T extends NDArray, K extends NDArray>(gpgpu: GPGPUContext, program: GPGPUProgram, inputs: Array<ArrayData<T>>, output: ArrayData<K>): GPGPUBinary;
export declare function runProgram<T extends NDArray, K extends NDArray>(binary: GPGPUBinary, inputs: Array<ArrayData<T>>, output: ArrayData<K>, customSetup?: (gpgpu: GPGPUContext, webGLProgram: WebGLProgram) => void): void;
export declare function makeShaderKey(program: GPGPUProgram, inputs: Array<ArrayData<NDArray>>, output: ArrayData<NDArray>): string;

@@ -15,5 +15,5 @@ "use strict";

var shapeInfo = {
logicalShape: input.shape,
texShape: input.getTextureShapeRC(),
textureType: input.getData().textureType
logicalShape: input.array.shape,
texShape: input.texData.texShape,
textureType: input.texData.textureType
};

@@ -24,5 +24,5 @@ return { name: program.variableNames[i], shapeInfo: shapeInfo };

var outShapeInfo = {
logicalShape: output.shape,
texShape: output.getTextureShapeRC(),
textureType: output.getData().textureType
logicalShape: output.array.shape,
texShape: output.texData.texShape,
textureType: output.texData.textureType
};

@@ -66,4 +66,4 @@ var source = shader_compiler.makeShader(inputInfos, outShapeInfo, userCode, program.supportsBroadcasting === true);

var texShapeA = s.texShape;
var shapeB = inputs[i].shape;
var texShapeB = inputs[i].getTextureShapeRC();
var shapeB = inputs[i].array.shape;
var texShapeB = inputs[i].texData.texShape;
if (!util.arraysEqual(shapeA, shapeB)) {

@@ -82,4 +82,4 @@ throw Error("Binary was compiled with different shapes than " +

validateBinaryAndProgram([binary.outShapeInfo], [output]);
var outTex = output.getTexture();
var outTexShape = output.getTextureShapeRC();
var outTex = output.texData.texture;
var outTexShape = output.texData.texShape;
var gpgpu = binary.gpgpu;

@@ -89,3 +89,3 @@ gpgpu.setOutputMatrixTexture(outTex, outTexShape[0], outTexShape[1]);

inputs.forEach(function (input, i) {
var tex = input.getTexture();
var tex = input.texData.texture;
var variableName = binary.program.variableNames[i];

@@ -107,3 +107,3 @@ var variableUniformLocation = binary.uniformLocations[variableName];

inputs.concat(output).forEach(function (x) {
keyInputs += x.shape + "_" + x.getTextureShapeRC();
keyInputs += x.array.shape + "_" + x.texData.texShape;
});

@@ -110,0 +110,0 @@ var keyUserCode = program.userCode;

@@ -1,2 +0,2 @@

import { MatrixOrientation } from '../backend';
import { MatrixOrientation } from '../types/matmul';
import { GPGPUProgram } from './gpgpu_math';

@@ -3,0 +3,0 @@ export declare class MatMulProgram implements GPGPUProgram {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var backend_1 = require("../backend");
var matmul_1 = require("../types/matmul");
var MatMulProgram = (function () {
function MatMulProgram(aShape, bShape, aOrient, bOrient) {
if (aOrient === void 0) { aOrient = backend_1.MatrixOrientation.REGULAR; }
if (bOrient === void 0) { bOrient = backend_1.MatrixOrientation.REGULAR; }
if (aOrient === void 0) { aOrient = matmul_1.MatrixOrientation.REGULAR; }
if (bOrient === void 0) { bOrient = matmul_1.MatrixOrientation.REGULAR; }
this.variableNames = ['matrixA', 'matrixB'];
var outerShapeA = (aOrient === backend_1.MatrixOrientation.REGULAR) ? aShape[0] : aShape[1];
var outerShapeB = (bOrient === backend_1.MatrixOrientation.REGULAR) ? bShape[1] : bShape[0];
var outerShapeA = (aOrient === matmul_1.MatrixOrientation.REGULAR) ? aShape[0] : aShape[1];
var outerShapeB = (bOrient === matmul_1.MatrixOrientation.REGULAR) ? bShape[1] : bShape[0];
this.outputShape = [outerShapeA, outerShapeB];
var sharedDim = (aOrient === backend_1.MatrixOrientation.REGULAR ? aShape[1] : aShape[0]);
var sharedDim = (aOrient === matmul_1.MatrixOrientation.REGULAR ? aShape[1] : aShape[0]);
var aSnippetFromOffset = function (vec4Offset, indexVar) {
return (aOrient === backend_1.MatrixOrientation.REGULAR) ?
return (aOrient === matmul_1.MatrixOrientation.REGULAR) ?
"aRow, " + indexVar + " + " + vec4Offset :

@@ -19,3 +19,3 @@ indexVar + " + " + vec4Offset + ", aRow";

var bSnippetFromOffset = function (vec4Offset, indexVar) {
return (bOrient === backend_1.MatrixOrientation.REGULAR) ?
return (bOrient === matmul_1.MatrixOrientation.REGULAR) ?
indexVar + " + " + vec4Offset + ", bCol" :

@@ -22,0 +22,0 @@ "bCol, " + indexVar + " + " + vec4Offset;

@@ -1,2 +0,2 @@

import { MatrixOrientation } from '../backend';
import { MatrixOrientation } from '../types/matmul';
import { GPGPUContext } from './gpgpu_context';

@@ -3,0 +3,0 @@ export declare function getFragmentShaderSource(sharedDimension: number, aOrientation: MatrixOrientation, bOrientation: MatrixOrientation): string;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var backend_1 = require("../backend");
var matmul_1 = require("../types/matmul");
var gpgpu_context_1 = require("./gpgpu_context");

@@ -8,11 +8,11 @@ var webgl_util = require("./webgl_util");

var sharedDimensionPacked = Math.ceil(sharedDimension / 2);
var aSample = (aOrientation === backend_1.MatrixOrientation.REGULAR) ?
var aSample = (aOrientation === matmul_1.MatrixOrientation.REGULAR) ?
'center, resultUV.t' :
'resultUV.t, center';
var bSample = (bOrientation === backend_1.MatrixOrientation.REGULAR) ?
var bSample = (bOrientation === matmul_1.MatrixOrientation.REGULAR) ?
'resultUV.s, center' :
'center, resultUV.s';
var aSwizzle = (aOrientation === backend_1.MatrixOrientation.REGULAR) ? ['a.xxzz', 'a.yyww'] :
var aSwizzle = (aOrientation === matmul_1.MatrixOrientation.REGULAR) ? ['a.xxzz', 'a.yyww'] :
['a.xxyy', 'a.zzww'];
var bSwizzle = (bOrientation === backend_1.MatrixOrientation.REGULAR) ? ['b.xyxy', 'b.zwzw'] :
var bSwizzle = (bOrientation === matmul_1.MatrixOrientation.REGULAR) ? ['b.xyxy', 'b.zwzw'] :
['b.xzxz', 'b.ywyw'];

@@ -33,11 +33,11 @@ return "\n precision highp float;\n uniform sampler2D matrixA;\n uniform sampler2D matrixB;\n varying vec2 resultUV;\n\n const float sharedDimension = " + sharedDimensionPacked + ".0;\n\n vec4 dot2x2ARowBCol() {\n vec4 result = vec4(0, 0, 0, 0);\n for (int ii = 0; ii < " + sharedDimensionPacked + "; ii++) {\n float i = float(ii);\n float center = (i + 0.5) / sharedDimension;\n vec4 a = texture2D(matrixA, vec2(" + aSample + "));\n vec4 b = texture2D(matrixB, vec2(" + bSample + "));\n result +=\n (" + aSwizzle[0] + " * " + bSwizzle[0] + ") + (" + aSwizzle[1] + " * " + bSwizzle[1] + ");\n }\n return result;\n }\n\n void main() {\n gl_FragColor = dot2x2ARowBCol();\n }";

function uploadMultiplyMatrixPackedDownload(a, aShapeRowCol, b, bShapeRowCol, aOrientation, bOrientation) {
if (aOrientation === void 0) { aOrientation = backend_1.MatrixOrientation.REGULAR; }
if (bOrientation === void 0) { bOrientation = backend_1.MatrixOrientation.REGULAR; }
var resultNumRows = (aOrientation === backend_1.MatrixOrientation.REGULAR) ?
if (aOrientation === void 0) { aOrientation = matmul_1.MatrixOrientation.REGULAR; }
if (bOrientation === void 0) { bOrientation = matmul_1.MatrixOrientation.REGULAR; }
var resultNumRows = (aOrientation === matmul_1.MatrixOrientation.REGULAR) ?
aShapeRowCol[0] :
aShapeRowCol[1];
var resultNumCols = (bOrientation === backend_1.MatrixOrientation.REGULAR) ?
var resultNumCols = (bOrientation === matmul_1.MatrixOrientation.REGULAR) ?
bShapeRowCol[1] :
bShapeRowCol[0];
var sharedDimension = (aOrientation === backend_1.MatrixOrientation.REGULAR) ?
var sharedDimension = (aOrientation === matmul_1.MatrixOrientation.REGULAR) ?
aShapeRowCol[1] :

@@ -44,0 +44,0 @@ aShapeRowCol[0];

@@ -0,1 +1,2 @@

import { DataTypes } from '../../ndarray';
export declare enum TextureType {

@@ -5,2 +6,9 @@ DEFAULT = 0,

}
export interface TextureData {
texture: WebGLTexture;
texShape: [number, number];
textureType: TextureType;
dtype: keyof DataTypes;
numChannels?: number;
}
export declare function getUnpackedMatrixTextureShapeWidthHeight(rows: number, columns: number): [number, number];

@@ -7,0 +15,0 @@ export declare function getUnpackedArraySizeFromMatrixSize(matrixSize: number, channelsPerTexture: number): number;

@@ -1,2 +0,6 @@

import { MathBackend, MatrixOrientation } from './backends/backend';
import { Backends } from '../environment';
import { NDArrayStorage } from './backends/backend';
import { MathBackend } from './backends/backend';
import { BackendEngine } from './backends/backend_engine';
import { MatrixOrientation } from './backends/types/matmul';
import { Array1D, Array2D, Array3D, Array4D, DataTypes, NDArray, Scalar } from './ndarray';

@@ -11,5 +15,18 @@ import { SumTypes } from './types';

}
export declare abstract class NDArrayMath {
protected backend: MathBackend;
export interface NDArrayManager {
getNumArrays(): number;
register<T extends keyof DataTypes>(a: NDArray<T>): void;
}
export declare class NDArrayMath implements NDArrayStorage, NDArrayManager {
private safeMode;
protected backendEngine: BackendEngine;
private numArrays;
private backend;
private customBackend;
getNumArrays(): number;
register<T extends keyof DataTypes>(a: NDArray<T>): void;
writePixels(id: number, pixels: ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement, numChannels: number): void;
write<T extends keyof DataTypes>(id: number, values: DataTypes[T], dtype: T, shape: number[]): void;
readSync<T extends keyof DataTypes>(id: number): DataTypes[T];
read<T extends keyof DataTypes>(id: number): Promise<DataTypes[T]>;
private ndarrayScopes;

@@ -19,4 +36,3 @@ private activeScope;

private activeScopeNDArraysToKeep;
private debugMode;
constructor(backend: MathBackend, safeMode: boolean);
constructor(backend: Backends | MathBackend, safeMode: boolean);
scope<T extends ScopeResult>(scopeFn: (keep: <D1 extends keyof DataTypes, T1 extends NDArray<D1>>(ndarray: T1) => T1, track: <D2 extends keyof DataTypes, T2 extends NDArray<D2>>(ndarray: T2) => T2) => T): T;

@@ -29,3 +45,2 @@ enableDebugMode(): void;

keep<T extends NDArray>(result: T): T;
private checkForNaN(vals, dtype, name);
track<G extends keyof DataTypes, T extends NDArray<G>>(result: T): T;

@@ -39,38 +54,39 @@ dispose(): void;

outerProduct(v1: Array1D, v2: Array1D): Array2D;
clone<T extends NDArray>(ndarray: T): T;
clone<T extends NDArray>(x: T): T;
reshape<T1 extends NDArray, T2 extends NDArray>(ndarray: T1, newShape: number[]): T2;
slice1D(input: Array1D, begin: number, size: number): Array1D;
slice2D(input: Array2D, begin: [number, number], size: [number, number]): Array2D;
slice3D(input: Array3D, begin: [number, number, number], size: [number, number, number]): Array3D;
slice4D(input: Array4D, begin: [number, number, number, number], size: [number, number, number, number]): Array4D;
copy2D(source: Array2D, sourceBegin: [number, number], sourceSize: [number, number], dest: Array2D, destBegin: [number, number], destSize: [number, number]): void;
slice1D(x: Array1D, begin: number, size: number): Array1D;
slice2D(x: Array2D, begin: [number, number], size: [number, number]): Array2D;
slice3D(x: Array3D, begin: [number, number, number], size: [number, number, number]): Array3D;
slice4D(x: Array4D, begin: [number, number, number, number], size: [number, number, number, number]): Array4D;
concat1D(a: Array1D, b: Array1D): Array1D;
concat2D(a: Array2D, b: Array2D, axis: number): Array2D;
concat3D(ndarray1: Array3D, ndarray2: Array3D, axis: number): Array3D;
concat4D(ndarray1: Array4D, ndarray2: Array4D, axis: number): Array4D;
concat3D(a: Array3D, b: Array3D, axis: number): Array3D;
concat4D(a: Array4D, b: Array4D, axis: number): Array4D;
logSumExp(input: NDArray, axis?: number | number[], keepDims?: boolean): NDArray;
sum<T extends keyof DataTypes>(input: NDArray<T>, axis?: number | number[], keepDims?: boolean): NDArray<SumTypes[T]>;
sum<T extends keyof DataTypes>(x: NDArray<T>, axis?: number | number[], keepDims?: boolean): NDArray<SumTypes[T]>;
mean(x: NDArray, axis?: number | number[], keepDims?: boolean): NDArray<'float32'>;
argMin(input: NDArray, axis?: number): NDArray<'int32'>;
argMax(input: NDArray, axis?: number): NDArray<'int32'>;
argMin(x: NDArray, axis?: number): NDArray<'int32'>;
argMax(x: NDArray, axis?: number): NDArray<'int32'>;
argMaxEquals(x1: NDArray, x2: NDArray): Scalar<'bool'>;
equal(x: NDArray, y: NDArray): NDArray<'bool'>;
equalStrict<D extends keyof DataTypes, T extends NDArray<D>>(x: T, y: T): NDArray<'bool'>;
topK(ndarray: NDArray, k: number): {
equal(a: NDArray, b: NDArray): NDArray<'bool'>;
equalStrict<D extends keyof DataTypes, T extends NDArray<D>>(a: T, b: T): NDArray<'bool'>;
topK(x: NDArray, k: number): {
values: Array1D;
indices: Array1D;
indices: Array1D<'int32'>;
};
min<G extends keyof DataTypes>(input: NDArray<G>, axis?: number | number[], keepDims?: boolean): NDArray<G>;
max<G extends keyof DataTypes>(input: NDArray<G>, axis?: number | number[], keepDims?: boolean): NDArray<G>;
min<G extends keyof DataTypes>(x: NDArray<G>, axis?: number | number[], keepDims?: boolean): NDArray<G>;
max<G extends keyof DataTypes>(x: NDArray<G>, axis?: number | number[], keepDims?: boolean): NDArray<G>;
softmax<T extends NDArray>(logits: T, dim?: number): T;
switchDim<T extends NDArray>(a: T, newDim: number[]): T;
tile<D extends keyof DataTypes, T extends NDArray<D>>(a: T, reps: number[]): T;
transpose<D extends keyof DataTypes, T extends NDArray<D>>(a: T, perm?: number[]): T;
tile<D extends keyof DataTypes, T extends NDArray<D>>(x: T, reps: number[]): T;
transpose<D extends keyof DataTypes, T extends NDArray<D>>(x: T, perm?: number[]): T;
scalarPlusArray<T extends NDArray>(c: Scalar, a: T): T;
scalarMinusArray<T extends NDArray>(c: Scalar, a: T): T;
arrayMinusScalar<T extends NDArray>(a: T, c: Scalar): T;
neg<T extends NDArray>(a: T): T;
neg<T extends NDArray>(x: T): T;
add<G extends keyof DataTypes>(a: NDArray<G>, b: NDArray<G>): NDArray<G>;
addStrict<D extends keyof DataTypes, T extends NDArray<D>>(a: T, b: T): T;
subtract<G extends keyof DataTypes>(a: NDArray<G>, b: NDArray<G>): NDArray<G>;
pow<G extends keyof DataTypes>(a: NDArray<G>, b: NDArray<'int32'>): NDArray<G>;
powStrict<G extends keyof DataTypes>(a: NDArray<G>, b: NDArray<'int32'>): NDArray<G>;
sub<G extends keyof DataTypes>(a: NDArray<G>, b: NDArray<G>): NDArray<G>;

@@ -85,29 +101,30 @@ subStrict<D extends keyof DataTypes, T extends NDArray<D>>(a: T, b: T): T;

arrayDividedByScalar<T extends NDArray>(a: T, c: Scalar): T;
ceil<T extends NDArray>(ndarray: T): T;
floor<T extends NDArray>(ndarray: T): T;
exp<T extends NDArray>(ndarray: T): T;
log<T extends NDArray>(ndarray: T): T;
sqrt<T extends NDArray>(ndarray: T): T;
ceil<T extends NDArray>(x: T): T;
floor<T extends NDArray>(x: T): T;
exp<T extends NDArray>(x: T): T;
log<T extends NDArray>(x: T): T;
sqrt<T extends NDArray>(x: T): T;
square<T extends NDArray>(x: T): T;
abs<T extends NDArray>(ndarray: T): T;
clip<T extends NDArray>(ndarray: T, min: number, max: number): T;
relu<T extends NDArray>(ndarray: T): T;
elu<T extends NDArray>(ndarray: T): T;
eluDer<T extends NDArray>(ndarray: T): T;
selu<T extends NDArray>(ndarray: T): T;
leakyRelu<T extends NDArray>(ndarray: T, alpha?: number): T;
sigmoid<T extends NDArray>(ndarray: T): T;
sin<T extends NDArray>(ndarray: T): T;
cos<T extends NDArray>(ndarray: T): T;
tan<T extends NDArray>(ndarray: T): T;
asin<T extends NDArray>(ndarray: T): T;
acos<T extends NDArray>(ndarray: T): T;
atan<T extends NDArray>(ndarray: T): T;
sinh<T extends NDArray>(ndarray: T): T;
cosh<T extends NDArray>(ndarray: T): T;
tanh<T extends NDArray>(ndarray: T): T;
step<T extends NDArray>(ndarray: T, alpha?: number): T;
abs<T extends NDArray>(x: T): T;
clip<T extends NDArray>(x: T, min: number, max: number): T;
relu<T extends NDArray>(x: T): T;
elu<T extends NDArray>(x: T): T;
eluDer<T extends NDArray>(x: T): T;
selu<T extends NDArray>(x: T): T;
leakyRelu<T extends NDArray>(x: T, alpha?: number): T;
sigmoid<T extends NDArray>(x: T): T;
sin<T extends NDArray>(x: T): T;
cos<T extends NDArray>(x: T): T;
tan<T extends NDArray>(x: T): T;
asin<T extends NDArray>(x: T): T;
acos<T extends NDArray>(x: T): T;
atan<T extends NDArray>(x: T): T;
sinh<T extends NDArray>(x: T): T;
cosh<T extends NDArray>(x: T): T;
tanh<T extends NDArray>(x: T): T;
step<T extends NDArray>(x: T, alpha?: number): T;
scaledArrayAdd<T extends NDArray>(c1: Scalar, a: T, c2: Scalar, b: T): T;
scalarTimesArray<T extends NDArray>(c: Scalar, a: T): T;
elementWiseMulBroadcast(a: Array2D, b: Array2D): Array2D;
conv1d<T extends NDArray>(input: T, filter: Array3D, bias: Array1D | null, stride: number, pad: 'valid' | 'same' | number): T;
conv2d<T extends NDArray>(input: T, filter: Array4D, bias: Array1D | null, strides: [number, number] | number, pad: 'valid' | 'same' | number): T;

@@ -134,2 +151,3 @@ conv2dDerInput<T extends NDArray>(inShape: [number, number, number, number] | [number, number, number], dy: T, filter: Array4D, strides: [number, number] | number, pad: 'valid' | 'same' | number): T;

};
disposeData(id: number): void;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var environment_1 = require("../environment");
var util = require("../util");
var axis_util = require("./axis_util");
var backend_1 = require("./backends/backend");
var backend_engine_1 = require("./backends/backend_engine");
var matmul_1 = require("./backends/types/matmul");
var broadcast_util = require("./broadcast_util");
var concat_util = require("./concat_util");
var conv_util = require("./conv_util");
var copy2d_util = require("./copy2d_util");
var ndarray_1 = require("./ndarray");

@@ -14,9 +16,37 @@ var slice_util = require("./slice_util");

function NDArrayMath(backend, safeMode) {
this.backend = backend;
this.safeMode = safeMode;
this.numArrays = 0;
this.customBackend = false;
this.ndarrayScopes = [];
this.ndarraysToKeep = [];
this.activeScopeNDArraysToKeep = [];
this.debugMode = false;
if (typeof backend === 'string') {
this.backend = backend_1.BACKEND_REGISTRY[backend];
}
else {
this.customBackend = true;
this.backend = backend;
}
this.backendEngine = new backend_engine_1.BackendEngine(this.backend);
environment_1.ENV.setGlobalMath(this);
}
NDArrayMath.prototype.getNumArrays = function () {
return this.numArrays;
};
NDArrayMath.prototype.register = function (a) {
this.track(a);
this.numArrays++;
};
NDArrayMath.prototype.writePixels = function (id, pixels, numChannels) {
this.backend.writePixels(id, pixels, numChannels);
};
NDArrayMath.prototype.write = function (id, values, dtype, shape) {
this.backend.write(id, values, dtype, shape);
};
NDArrayMath.prototype.readSync = function (id) {
return this.backend.readSync(id);
};
NDArrayMath.prototype.read = function (id) {
return this.backend.read(id);
};
NDArrayMath.prototype.scope = function (scopeFn) {

@@ -38,3 +68,3 @@ var _this = this;

NDArrayMath.prototype.enableDebugMode = function () {
this.debugMode = true;
this.backendEngine.enableDebugMode();
console.warn('Debugging mode is ON. The output of every math call will ' +

@@ -97,3 +127,3 @@ 'be downloaded to CPU and checked for NaNs. ' +

for (var i = 0; i < ndarrayList.length; i++) {
if (ndarrayList[i].getData() === ndarray.getData()) {
if (ndarrayList[i].id === ndarray.id) {
return true;

@@ -117,9 +147,2 @@ }

};
NDArrayMath.prototype.checkForNaN = function (vals, dtype, name) {
for (var i = 0; i < vals.length; i++) {
if (util.isValNaN(vals[i], dtype)) {
throw Error("The result of the last math." + name + " has NaNs.");
}
}
};
NDArrayMath.prototype.track = function (result) {

@@ -138,9 +161,12 @@ if (this.activeScope == null) {

};
NDArrayMath.prototype.dispose = function () { };
NDArrayMath.prototype.dispose = function () {
if (this.customBackend) {
this.backend.dispose();
}
};
NDArrayMath.prototype.matMul = function (a, b, aOrientation, bOrientation) {
var _this = this;
if (aOrientation === void 0) { aOrientation = backend_1.MatrixOrientation.REGULAR; }
if (bOrientation === void 0) { bOrientation = backend_1.MatrixOrientation.REGULAR; }
var innerShapeA = (aOrientation === backend_1.MatrixOrientation.REGULAR) ? a.shape[1] : a.shape[0];
var innerShapeB = (bOrientation === backend_1.MatrixOrientation.REGULAR) ? b.shape[0] : b.shape[1];
if (aOrientation === void 0) { aOrientation = matmul_1.MatrixOrientation.REGULAR; }
if (bOrientation === void 0) { bOrientation = matmul_1.MatrixOrientation.REGULAR; }
var innerShapeA = (aOrientation === matmul_1.MatrixOrientation.REGULAR) ? a.shape[1] : a.shape[0];
var innerShapeB = (bOrientation === matmul_1.MatrixOrientation.REGULAR) ? b.shape[0] : b.shape[1];
util.assert(a.rank === 2 && b.rank === 2, "Error in matMul: inputs must be rank 2, got ranks " + a.rank +

@@ -150,23 +176,8 @@ (" and " + b.rank + "."));

(innerShapeB + ") of NDArrays with shapes " + a.shape + " and ") +
(b.shape + " and orientations " + backend_1.MatrixOrientation[aOrientation]) +
(" and " + backend_1.MatrixOrientation[bOrientation] + " must match."));
return this.executeOp('matMul', function () { return _this.backend.matMul(a, b, aOrientation, bOrientation); });
(b.shape + " and orientations " + matmul_1.MatrixOrientation[aOrientation]) +
(" and " + matmul_1.MatrixOrientation[bOrientation] + " must match."));
return this.backendEngine.executeKernel('MatMul', { inputs: { a: a, b: b }, args: { aOrientation: aOrientation, bOrientation: bOrientation } });
};
NDArrayMath.prototype.executeOp = function (name, f) {
var start;
if (this.debugMode) {
start = performance.now();
}
var result = f();
if (this.debugMode) {
var vals = result.getValues();
var time = util.rightPad(performance.now() - start + "ms", 9);
var paddedName = util.rightPad(name, 25);
var rank = result.rank;
var size = result.size;
var shape = util.rightPad(result.shape.toString(), 14);
console.log("%c" + paddedName + "\t%c" + time + "\t%c" + rank + "D " + shape + "\t%c" + size, 'font-weight:bold', 'color:red', 'color:blue', 'color: orange');
this.checkForNaN(vals, result.dtype, name);
}
return this.track(result);
return f();
};

@@ -204,5 +215,4 @@ NDArrayMath.prototype.vectorTimesMatrix = function (v, matrix) {

};
NDArrayMath.prototype.clone = function (ndarray) {
var _this = this;
return this.executeOp('clone', function () { return _this.backend.clone(ndarray); });
NDArrayMath.prototype.clone = function (x) {
return this.backendEngine.executeKernel('Clone', { inputs: { x: x } });
};

@@ -214,57 +224,33 @@ NDArrayMath.prototype.reshape = function (ndarray, newShape) {

};
NDArrayMath.prototype.slice1D = function (input, begin, size) {
var _this = this;
slice_util.assertParamsValid(input, [begin], [size]);
return this.executeOp('slice1D', function () { return _this.backend.slice1D(input, begin, size); });
NDArrayMath.prototype.slice1D = function (x, begin, size) {
slice_util.assertParamsValid(x, [begin], [size]);
return this.backendEngine.executeKernel('Slice1D', { inputs: { x: x }, args: { begin: begin, size: size } });
};
NDArrayMath.prototype.slice2D = function (input, begin, size) {
var _this = this;
slice_util.assertParamsValid(input, begin, size);
return this.executeOp('slice2D', function () { return _this.backend.slice2D(input, begin, size); });
NDArrayMath.prototype.slice2D = function (x, begin, size) {
slice_util.assertParamsValid(x, begin, size);
return this.backendEngine.executeKernel('Slice2D', { inputs: { x: x }, args: { begin: begin, size: size } });
};
NDArrayMath.prototype.slice3D = function (input, begin, size) {
var _this = this;
slice_util.assertParamsValid(input, begin, size);
return this.executeOp('slice3D', function () { return _this.backend.slice3D(input, begin, size); });
NDArrayMath.prototype.slice3D = function (x, begin, size) {
slice_util.assertParamsValid(x, begin, size);
return this.backendEngine.executeKernel('Slice3D', { inputs: { x: x }, args: { begin: begin, size: size } });
};
NDArrayMath.prototype.slice4D = function (input, begin, size) {
var _this = this;
slice_util.assertParamsValid(input, begin, size);
return this.executeOp('slice4D', function () { return _this.backend.slice4D(input, begin, size); });
NDArrayMath.prototype.slice4D = function (x, begin, size) {
slice_util.assertParamsValid(x, begin, size);
return this.backendEngine.executeKernel('Slice4D', { inputs: { x: x }, args: { begin: begin, size: size } });
};
NDArrayMath.prototype.copy2D = function (source, sourceBegin, sourceSize, dest, destBegin, destSize) {
var _this = this;
util.assert(sourceBegin[0] + sourceSize[0] <= source.shape[0] &&
sourceBegin[1] + sourceSize[1] <= source.shape[1], "Error in copy2D: requested source start position " + sourceBegin + " " +
("and source size " + sourceSize + " would overflow source NDArray") +
("of shape " + source.shape + "."));
util.assert(destBegin[0] + destSize[0] <= dest.shape[0] &&
destBegin[1] + destSize[1] <= dest.shape[1], "Error in copy2D: requested dest start position " + destBegin + " " +
("and source size " + destSize + " would overflow dest NDArray of") +
("shape " + dest.shape + "."));
copy2d_util.validateShapes(sourceSize, destSize);
this.executeOp('copy2D', function () {
_this.backend.copy2D(source, sourceBegin, sourceSize, dest, destBegin, destSize);
return dest;
});
};
NDArrayMath.prototype.concat1D = function (a, b) {
var _this = this;
concat_util.assertParams(a.shape, b.shape, 0);
return this.executeOp('concat1D', function () { return _this.backend.concat1D(a, b); });
return this.backendEngine.executeKernel('Concat1D', { inputs: { a: a, b: b } });
};
NDArrayMath.prototype.concat2D = function (a, b, axis) {
var _this = this;
concat_util.assertParams(a.shape, b.shape, axis);
return this.executeOp('concat2D', function () { return _this.backend.concat2D(a, b, axis); });
return this.backendEngine.executeKernel('Concat2D', { inputs: { a: a, b: b }, args: { axis: axis } });
};
NDArrayMath.prototype.concat3D = function (ndarray1, ndarray2, axis) {
var _this = this;
concat_util.assertParams(ndarray1.shape, ndarray2.shape, axis);
return this.executeOp('concat3D', function () { return _this.backend.concat3D(ndarray1, ndarray2, axis); });
NDArrayMath.prototype.concat3D = function (a, b, axis) {
concat_util.assertParams(a.shape, b.shape, axis);
return this.backendEngine.executeKernel('Concat3D', { inputs: { a: a, b: b }, args: { axis: axis } });
};
NDArrayMath.prototype.concat4D = function (ndarray1, ndarray2, axis) {
var _this = this;
concat_util.assertParams(ndarray1.shape, ndarray2.shape, axis);
return this.executeOp('concat4D', function () { return _this.backend.concat4D(ndarray1, ndarray2, axis); });
NDArrayMath.prototype.concat4D = function (a, b, axis) {
concat_util.assertParams(a.shape, b.shape, axis);
return this.backendEngine.executeKernel('Concat4D', { inputs: { a: a, b: b }, args: { axis: axis } });
};

@@ -290,15 +276,15 @@ NDArrayMath.prototype.logSumExp = function (input, axis, keepDims) {

};
NDArrayMath.prototype.sum = function (input, axis, keepDims) {
NDArrayMath.prototype.sum = function (x, axis, keepDims) {
var _this = this;
if (axis === void 0) { axis = null; }
if (keepDims === void 0) { keepDims = false; }
var origAxes = axis_util.parseAxisParam(axis, input.shape);
var origAxes = axis_util.parseAxisParam(axis, x.shape);
var axes = origAxes;
var permutedAxes = axis_util.getPermutedAxes(axes, input.rank);
var permutedAxes = axis_util.getPermutedAxes(axes, x.rank);
return this.executeOp('sum', function () {
if (permutedAxes != null) {
input = _this.transpose(input, permutedAxes);
axes = axis_util.getInnerMostAxes(axes.length, input.rank);
x = _this.transpose(x, permutedAxes);
axes = axis_util.getInnerMostAxes(axes.length, x.rank);
}
var res = _this.backend.sum(input, axes);
var res = _this.backendEngine.executeKernel('Sum', { inputs: { x: x }, args: { axes: axes } });
if (keepDims) {

@@ -326,26 +312,26 @@ var newShape = axis_util.expandShapeToKeepDim(res.shape, origAxes);

};
NDArrayMath.prototype.argMin = function (input, axis) {
NDArrayMath.prototype.argMin = function (x, axis) {
var _this = this;
if (axis === void 0) { axis = null; }
var axes = axis_util.parseAxisParam(axis, input.shape);
var permutedAxes = axis_util.getPermutedAxes(axes, input.rank);
var axes = axis_util.parseAxisParam(axis, x.shape);
var permutedAxes = axis_util.getPermutedAxes(axes, x.rank);
return this.executeOp('argMin', function () {
if (permutedAxes != null) {
input = _this.transpose(input, permutedAxes);
axes = axis_util.getInnerMostAxes(axes.length, input.rank);
x = _this.transpose(x, permutedAxes);
axes = axis_util.getInnerMostAxes(axes.length, x.rank);
}
return _this.backend.argMin(input, axes);
return _this.backendEngine.executeKernel('ArgMin', { inputs: { x: x }, args: { axes: axes } });
});
};
NDArrayMath.prototype.argMax = function (input, axis) {
NDArrayMath.prototype.argMax = function (x, axis) {
var _this = this;
if (axis === void 0) { axis = null; }
var axes = axis_util.parseAxisParam(axis, input.shape);
var permutedAxes = axis_util.getPermutedAxes(axes, input.rank);
var axes = axis_util.parseAxisParam(axis, x.shape);
var permutedAxes = axis_util.getPermutedAxes(axes, x.rank);
return this.executeOp('argMax', function () {
if (permutedAxes != null) {
input = _this.transpose(input, permutedAxes);
axes = axis_util.getInnerMostAxes(axes.length, input.rank);
x = _this.transpose(x, permutedAxes);
axes = axis_util.getInnerMostAxes(axes.length, x.rank);
}
return _this.backend.argMax(input, axes);
return _this.backendEngine.executeKernel('ArgMax', { inputs: { x: x }, args: { axes: axes } });
});

@@ -360,38 +346,36 @@ };

};
NDArrayMath.prototype.equal = function (x, y) {
var _this = this;
return this.executeOp('equal', function () { return _this.backend.equal(x, y); });
NDArrayMath.prototype.equal = function (a, b) {
return this.backendEngine.executeKernel('Equal', { inputs: { a: a, b: b } });
};
NDArrayMath.prototype.equalStrict = function (x, y) {
util.assertShapesMatch(x.shape, y.shape, 'Error in equalStrict: ');
return this.equal(x, y);
NDArrayMath.prototype.equalStrict = function (a, b) {
util.assertShapesMatch(a.shape, b.shape, 'Error in equalStrict: ');
return this.equal(a, b);
};
NDArrayMath.prototype.topK = function (ndarray, k) {
NDArrayMath.prototype.topK = function (x, k) {
var _this = this;
util.assert(k <= ndarray.size, "Error in topK: k value (" + k + ") must be less than size of input " +
("ndarray, got shape " + ndarray.shape + "."));
util.assert(k <= x.size, "Error in topK: k value (" + k + ") must be less than size of input " +
("ndarray, got shape " + x.shape + "."));
var values;
var indices;
this.executeOp('topK', function () {
values = _this.backend.topKValues(ndarray, k);
indices = _this.backend.topKIndices(ndarray, k);
values = _this.backendEngine.executeKernel('TopKValues', { inputs: { x: x }, args: { k: k } });
indices = _this.backendEngine.executeKernel('TopKIndices', { inputs: { x: x }, args: { k: k } });
return values;
});
var result = { values: values, indices: indices };
this.track(result.indices);
return result;
};
NDArrayMath.prototype.min = function (input, axis, keepDims) {
NDArrayMath.prototype.min = function (x, axis, keepDims) {
var _this = this;
if (axis === void 0) { axis = null; }
if (keepDims === void 0) { keepDims = false; }
var origAxes = axis_util.parseAxisParam(axis, input.shape);
var origAxes = axis_util.parseAxisParam(axis, x.shape);
var axes = origAxes;
var permutedAxes = axis_util.getPermutedAxes(axes, input.rank);
var permutedAxes = axis_util.getPermutedAxes(axes, x.rank);
return this.executeOp('min', function () {
if (permutedAxes != null) {
input = _this.transpose(input, permutedAxes);
axes = axis_util.getInnerMostAxes(axes.length, input.rank);
x = _this.transpose(x, permutedAxes);
axes = axis_util.getInnerMostAxes(axes.length, x.rank);
}
var res = _this.backend.min(input, axes);
var res = _this.backendEngine.executeKernel('Min', { inputs: { x: x }, args: { axes: axes } });
if (keepDims) {

@@ -404,15 +388,15 @@ var newShape = axis_util.expandShapeToKeepDim(res.shape, origAxes);

};
NDArrayMath.prototype.max = function (input, axis, keepDims) {
NDArrayMath.prototype.max = function (x, axis, keepDims) {
var _this = this;
if (axis === void 0) { axis = null; }
if (keepDims === void 0) { keepDims = false; }
var origAxes = axis_util.parseAxisParam(axis, input.shape);
var origAxes = axis_util.parseAxisParam(axis, x.shape);
var axes = origAxes;
var permutedAxes = axis_util.getPermutedAxes(axes, input.rank);
var permutedAxes = axis_util.getPermutedAxes(axes, x.rank);
return this.executeOp('max', function () {
if (permutedAxes != null) {
input = _this.transpose(input, permutedAxes);
axes = axis_util.getInnerMostAxes(axes.length, input.rank);
x = _this.transpose(x, permutedAxes);
axes = axis_util.getInnerMostAxes(axes.length, x.rank);
}
var res = _this.backend.max(input, axes);
var res = _this.backendEngine.executeKernel('Max', { inputs: { x: x }, args: { axes: axes } });
if (keepDims) {

@@ -446,16 +430,14 @@ var newShape = axis_util.expandShapeToKeepDim(res.shape, origAxes);

};
NDArrayMath.prototype.tile = function (a, reps) {
var _this = this;
util.assert(a.rank === reps.length, "Error in transpose: rank of input " + a.rank + " " +
NDArrayMath.prototype.tile = function (x, reps) {
util.assert(x.rank === reps.length, "Error in transpose: rank of input " + x.rank + " " +
("must match length of reps " + reps + "."));
return this.executeOp('tile', function () { return _this.backend.tile(a, reps); });
return this.backendEngine.executeKernel('Tile', { inputs: { x: x }, args: { reps: reps } });
};
NDArrayMath.prototype.transpose = function (a, perm) {
var _this = this;
NDArrayMath.prototype.transpose = function (x, perm) {
if (perm == null) {
perm = a.shape.map(function (s, i) { return i; }).reverse();
perm = x.shape.map(function (s, i) { return i; }).reverse();
}
util.assert(a.rank === perm.length, "Error in transpose: rank of input " + a.rank + " " +
util.assert(x.rank === perm.length, "Error in transpose: rank of input " + x.rank + " " +
("must match length of perm " + perm + "."));
return this.executeOp('transpose', function () { return _this.backend.transpose(a, perm); });
return this.backendEngine.executeKernel('Transpose', { inputs: { x: x }, args: { perm: perm } });
};

@@ -477,10 +459,8 @@ NDArrayMath.prototype.scalarPlusArray = function (c, a) {

};
NDArrayMath.prototype.neg = function (a) {
var _this = this;
return this.executeOp('neg', function () { return _this.backend.neg(a); });
NDArrayMath.prototype.neg = function (x) {
return this.backendEngine.executeKernel('Neg', { inputs: { x: x } });
};
NDArrayMath.prototype.add = function (a, b) {
var _this = this;
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
return this.executeOp('add', function () { return _this.backend.add(a, b); });
return this.backendEngine.executeKernel('Add', { inputs: { a: a, b: b } });
};

@@ -492,6 +472,14 @@ NDArrayMath.prototype.addStrict = function (a, b) {

NDArrayMath.prototype.subtract = function (a, b) {
var _this = this;
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
return this.executeOp('subtract', function () { return _this.backend.subtract(a, b); });
return this.backendEngine.executeKernel('Sub', { inputs: { a: a, b: b } });
};
NDArrayMath.prototype.pow = function (a, b) {
util.assert(b.dtype === 'int32', 'only supports int32 data type for the exponent parameter.');
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
return this.backendEngine.executeKernel('Pow', { inputs: { a: a, b: b } });
};
NDArrayMath.prototype.powStrict = function (a, b) {
util.assertShapesMatch(a.shape, b.shape, 'Error in powStrict: ');
return this.pow(a, b);
};
NDArrayMath.prototype.sub = function (a, b) {

@@ -505,5 +493,4 @@ return this.subtract(a, b);

NDArrayMath.prototype.multiply = function (a, b) {
var _this = this;
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
return this.executeOp('multiply', function () { return _this.backend.multiply(a, b); });
return this.backendEngine.executeKernel('Mul', { inputs: { a: a, b: b } });
};

@@ -518,5 +505,4 @@ NDArrayMath.prototype.elementWiseMul = function (a, b) {

NDArrayMath.prototype.divide = function (a, b) {
var _this = this;
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
return this.executeOp('divide', function () { return _this.backend.divide(a, b); });
return this.backendEngine.executeKernel('Div', { inputs: { a: a, b: b } });
};

@@ -537,101 +523,77 @@ NDArrayMath.prototype.divideStrict = function (a, b) {

};
NDArrayMath.prototype.ceil = function (ndarray) {
var _this = this;
return this.executeOp('ceil', function () { return _this.backend.ceil(ndarray); });
NDArrayMath.prototype.ceil = function (x) {
return this.backendEngine.executeKernel('Ceil', { inputs: { x: x } });
};
NDArrayMath.prototype.floor = function (ndarray) {
var _this = this;
return this.executeOp('floor', function () { return _this.backend.floor(ndarray); });
NDArrayMath.prototype.floor = function (x) {
return this.backendEngine.executeKernel('Floor', { inputs: { x: x } });
};
NDArrayMath.prototype.exp = function (ndarray) {
var _this = this;
return this.executeOp('exp', function () { return _this.backend.exp(ndarray); });
NDArrayMath.prototype.exp = function (x) {
return this.backendEngine.executeKernel('Exp', { inputs: { x: x } });
};
NDArrayMath.prototype.log = function (ndarray) {
var _this = this;
return this.executeOp('log', function () { return _this.backend.log(ndarray); });
NDArrayMath.prototype.log = function (x) {
return this.backendEngine.executeKernel('Log', { inputs: { x: x } });
};
NDArrayMath.prototype.sqrt = function (ndarray) {
var _this = this;
return this.executeOp('sqrt', function () { return _this.backend.sqrt(ndarray); });
NDArrayMath.prototype.sqrt = function (x) {
return this.backendEngine.executeKernel('Sqrt', { inputs: { x: x } });
};
NDArrayMath.prototype.square = function (x) {
var _this = this;
return this.executeOp('square', function () { return _this.backend.square(x); });
return this.backendEngine.executeKernel('Square', { inputs: { x: x } });
};
NDArrayMath.prototype.abs = function (ndarray) {
var _this = this;
return this.executeOp('abs', function () { return _this.backend.abs(ndarray); });
NDArrayMath.prototype.abs = function (x) {
return this.backendEngine.executeKernel('Abs', { inputs: { x: x } });
};
NDArrayMath.prototype.clip = function (ndarray, min, max) {
var _this = this;
NDArrayMath.prototype.clip = function (x, min, max) {
util.assert((min <= max), "Error in clip: min (" + min + ") must be" +
("less than or equal to max (" + max + ")."));
return this.executeOp('clip', function () { return _this.backend.clip(ndarray, min, max); });
return this.backendEngine.executeKernel('Clip', { inputs: { x: x }, args: { min: min, max: max } });
};
NDArrayMath.prototype.relu = function (ndarray) {
var _this = this;
return this.executeOp('relu', function () { return _this.backend.relu(ndarray); });
NDArrayMath.prototype.relu = function (x) {
return this.backendEngine.executeKernel('Relu', { inputs: { x: x } });
};
NDArrayMath.prototype.elu = function (ndarray) {
var _this = this;
return this.executeOp('elu', function () { return _this.backend.elu(ndarray); });
NDArrayMath.prototype.elu = function (x) {
return this.backendEngine.executeKernel('Elu', { inputs: { x: x } });
};
NDArrayMath.prototype.eluDer = function (ndarray) {
var _this = this;
return this.executeOp('eluDer', function () { return _this.backend.eluDer(ndarray); });
NDArrayMath.prototype.eluDer = function (x) {
return this.backendEngine.executeKernel('EluDer', { inputs: { x: x } });
};
NDArrayMath.prototype.selu = function (ndarray) {
var _this = this;
return this.executeOp('selu', function () { return _this.backend.selu(ndarray); });
NDArrayMath.prototype.selu = function (x) {
return this.backendEngine.executeKernel('Selu', { inputs: { x: x } });
};
NDArrayMath.prototype.leakyRelu = function (ndarray, alpha) {
var _this = this;
NDArrayMath.prototype.leakyRelu = function (x, alpha) {
if (alpha === void 0) { alpha = 0.2; }
return this.executeOp('leakyRelu', function () { return _this.backend.leakyRelu(ndarray, alpha); });
return this.backendEngine.executeKernel('LeakyRelu', { inputs: { x: x }, args: { alpha: alpha } });
};
NDArrayMath.prototype.sigmoid = function (ndarray) {
var _this = this;
return this.executeOp('sigmoid', function () { return _this.backend.sigmoid(ndarray); });
NDArrayMath.prototype.sigmoid = function (x) {
return this.backendEngine.executeKernel('Sigmoid', { inputs: { x: x } });
};
NDArrayMath.prototype.sin = function (ndarray) {
var _this = this;
return this.executeOp('sin', function () { return _this.backend.sin(ndarray); });
NDArrayMath.prototype.sin = function (x) {
return this.backendEngine.executeKernel('Sin', { inputs: { x: x } });
};
NDArrayMath.prototype.cos = function (ndarray) {
var _this = this;
return this.executeOp('cos', function () { return _this.backend.cos(ndarray); });
NDArrayMath.prototype.cos = function (x) {
return this.backendEngine.executeKernel('Cos', { inputs: { x: x } });
};
NDArrayMath.prototype.tan = function (ndarray) {
var _this = this;
return this.executeOp('tan', function () { return _this.backend.tan(ndarray); });
NDArrayMath.prototype.tan = function (x) {
return this.backendEngine.executeKernel('Tan', { inputs: { x: x } });
};
NDArrayMath.prototype.asin = function (ndarray) {
var _this = this;
return this.executeOp('asin', function () { return _this.backend.asin(ndarray); });
NDArrayMath.prototype.asin = function (x) {
return this.backendEngine.executeKernel('Asin', { inputs: { x: x } });
};
NDArrayMath.prototype.acos = function (ndarray) {
var _this = this;
return this.executeOp('acos', function () { return _this.backend.acos(ndarray); });
NDArrayMath.prototype.acos = function (x) {
return this.backendEngine.executeKernel('Acos', { inputs: { x: x } });
};
NDArrayMath.prototype.atan = function (ndarray) {
var _this = this;
return this.executeOp('atan', function () { return _this.backend.atan(ndarray); });
NDArrayMath.prototype.atan = function (x) {
return this.backendEngine.executeKernel('Atan', { inputs: { x: x } });
};
NDArrayMath.prototype.sinh = function (ndarray) {
var _this = this;
return this.executeOp('sinh', function () { return _this.backend.sinh(ndarray); });
NDArrayMath.prototype.sinh = function (x) {
return this.backendEngine.executeKernel('Sinh', { inputs: { x: x } });
};
NDArrayMath.prototype.cosh = function (ndarray) {
var _this = this;
return this.executeOp('cosh', function () { return _this.backend.cosh(ndarray); });
NDArrayMath.prototype.cosh = function (x) {
return this.backendEngine.executeKernel('Cosh', { inputs: { x: x } });
};
NDArrayMath.prototype.tanh = function (ndarray) {
var _this = this;
return this.executeOp('tanh', function () { return _this.backend.tanh(ndarray); });
NDArrayMath.prototype.tanh = function (x) {
return this.backendEngine.executeKernel('Tanh', { inputs: { x: x } });
};
NDArrayMath.prototype.step = function (ndarray, alpha) {
var _this = this;
NDArrayMath.prototype.step = function (x, alpha) {
if (alpha === void 0) { alpha = 0.0; }
return this.executeOp('step', function () { return _this.backend.step(ndarray, alpha); });
return this.backendEngine.executeKernel('Step', { inputs: { x: x }, args: { alpha: alpha } });
};

@@ -645,3 +607,7 @@ NDArrayMath.prototype.scaledArrayAdd = function (c1, a, c2, b) {

util.assertShapesMatch(a.shape, b.shape, 'Error in scaledArrayAdd: ');
return this.executeOp('scaledArrayAdd', function () { return _this.backend.scaledArrayAdd(c1, a, c2, b); });
return this.executeOp('scaledArrayAdd', function () {
return _this.scope(function () {
return _this.add(_this.multiply(c1, a), _this.multiply(c2, b));
});
});
};

@@ -660,2 +626,30 @@ NDArrayMath.prototype.scalarTimesArray = function (c, a) {

};
NDArrayMath.prototype.conv1d = function (input, filter, bias, stride, pad) {
var _this = this;
var input3D = input;
var reshapedTo3D = false;
if (input.rank === 2) {
reshapedTo3D = true;
input3D = input.as3D(1, input.shape[0], input.shape[1]);
}
util.assert(input3D.rank === 3, "Error in conv1d: input must be rank 3, but got rank " + input3D.rank + ".");
util.assert(filter.rank === 3, "Error in conv1d: filter must be rank 3, but got rank " +
(filter.rank + "."));
if (bias != null) {
util.assert(bias.rank === 1, "Error in conv1d: bias must be rank 1, but got rank " +
(bias.rank + "."));
}
util.assert(input3D.shape[2] === filter.shape[1], "Error in conv1d: depth of input (" + input3D.shape[2] + ") must match " +
("input depth for filter " + filter.shape[1] + "."));
var filter4D = filter.as4D(1, filter.shape[0], filter.shape[1], filter.shape[2]);
var input4D = input3D.as4D(input3D.shape[0], 1, input3D.shape[1], input3D.shape[2]);
var strides = [1, stride];
return this.executeOp('Conv1D', function () {
var res = _this.conv2d(input4D, filter4D, bias, strides, pad);
if (reshapedTo3D) {
return res.as2D(res.shape[2], res.shape[3]);
}
return res.as3D(res.shape[0], res.shape[2], res.shape[3]);
});
};
NDArrayMath.prototype.conv2d = function (input, filter, bias, strides, pad) {

@@ -679,4 +673,4 @@ var _this = this;

var convInfo = conv_util.computeConv2DInfo(input4D.shape, filter.shape, strides, pad);
return this.executeOp('conv2d', function () {
var res = _this.backend.conv2d(input4D, filter, bias, convInfo);
return this.executeOp('Conv2D', function () {
var res = _this.backendEngine.executeKernel('Conv2D', { inputs: { x: input4D, filter: filter, bias: bias }, args: { convInfo: convInfo } });
if (reshapedTo4D) {

@@ -714,3 +708,3 @@ return res.as3D(res.shape[1], res.shape[2], res.shape[3]);

return this.executeOp('conv2dDerInput', function () {
var res = _this.backend.conv2dDerInput(dy4D, filter, convInfo);
var res = _this.backendEngine.executeKernel('Conv2DDerInput', { inputs: { dy: dy4D, filter: filter }, args: { convInfo: convInfo } });
if (reshapedTo4D) {

@@ -727,3 +721,3 @@ return res.as3D(res.shape[1], res.shape[2], res.shape[3]);

}
return this.track(this.backend.conv2dDerBias(dy4D));
return this.backendEngine.executeKernel('Conv2DDerBias', { inputs: { dy: dy4D } });
};

@@ -750,3 +744,3 @@ NDArrayMath.prototype.conv2dDerFilter = function (input, dy, filterShape, strides, pad) {

var convInfo = conv_util.computeConv2DInfo(input4D.shape, filterShape, strides, pad);
return this.track(this.backend.conv2dDerFilter(input4D, dy4D, convInfo));
return this.backendEngine.executeKernel('Conv2DDerFilter', { inputs: { x: input4D, dy: dy4D }, args: { convInfo: convInfo } });
};

@@ -778,3 +772,3 @@ NDArrayMath.prototype.conv2dTranspose = function (x, filter, outputShape, strides, pad) {

return this.executeOp('depthwiseConv2D', function () {
var res = _this.backend.depthwiseConv2D(input4D, filter, convInfo);
var res = _this.backendEngine.executeKernel('DepthwiseConv2D', { inputs: { x: input4D, filter: filter }, args: { convInfo: convInfo } });
if (reshapedTo4D) {

@@ -797,3 +791,3 @@ return res.as3D(res.shape[1], res.shape[2], res.shape[3]);

return this.executeOp('maxPool', function () {
var res = _this.backend.maxPool(input4D, convInfo);
var res = _this.backendEngine.executeKernel('MaxPool', { inputs: { x: input4D }, args: { convInfo: convInfo } });
if (reshapedTo4D) {

@@ -822,3 +816,3 @@ return res.as3D(res.shape[1], res.shape[2], res.shape[3]);

return this.executeOp('maxPoolBackprop', function () {
var res = _this.backend.maxPoolBackprop(dy4D, input4D, convInfo);
var res = _this.backendEngine.executeKernel('MaxPoolBackprop', { inputs: { dy: dy4D, x: input4D }, args: { convInfo: convInfo } });
if (reshapedTo4D) {

@@ -841,3 +835,3 @@ return res.as3D(res.shape[1], res.shape[2], res.shape[3]);

return this.executeOp('minPool', function () {
var res = _this.backend.minPool(input4D, convInfo);
var res = _this.backendEngine.executeKernel('MinPool', { inputs: { x: input4D }, args: { convInfo: convInfo } });
if (reshapedTo4D) {

@@ -860,3 +854,3 @@ return res.as3D(res.shape[1], res.shape[2], res.shape[3]);

return this.executeOp('avgPool', function () {
var res = _this.backend.avgPool(input4D, convInfo);
var res = _this.backendEngine.executeKernel('AvgPool', { inputs: { x: input4D }, args: { convInfo: convInfo } });
if (reshapedTo4D) {

@@ -869,3 +863,2 @@ return res.as3D(res.shape[1], res.shape[2], res.shape[3]);

NDArrayMath.prototype.resizeBilinear3D = function (x, newShape2D, alignCorners) {
var _this = this;
if (alignCorners === void 0) { alignCorners = false; }

@@ -875,6 +868,5 @@ util.assert(x.rank === 3, "Error in resizeBilinear3D: x must be rank 3 but got rank " + x.rank + ".");

(newShape2D + "."));
return this.executeOp('resizeBilinear3D', function () { return _this.backend.resizeBilinear3D(x, newShape2D, alignCorners); });
return this.backendEngine.executeKernel('ResizeBilinear3D', { inputs: { x: x }, args: { newShape2D: newShape2D, alignCorners: alignCorners } });
};
NDArrayMath.prototype.batchNormalization2D = function (x, mean, variance, varianceEpsilon, scale, offset) {
var _this = this;
if (varianceEpsilon === void 0) { varianceEpsilon = .001; }

@@ -895,6 +887,5 @@ util.assert(x.rank === 2, "Error in batchNormalization3D: x must be rank 3 but got rank " +

}
return this.executeOp('batchNorm2D', function () { return _this.backend.batchNormalization2D(x, mean, variance, varianceEpsilon, scale, offset); });
return this.backendEngine.executeKernel('BatchNorm2D', { inputs: { x: x, mean: mean, variance: variance, scale: scale, offset: offset }, args: { varianceEpsilon: varianceEpsilon } });
};
NDArrayMath.prototype.batchNormalization3D = function (x, mean, variance, varianceEpsilon, scale, offset) {
var _this = this;
if (varianceEpsilon === void 0) { varianceEpsilon = .001; }

@@ -915,3 +906,3 @@ util.assert(x.rank === 3, "Error in batchNormalization3D: x must be rank 3 but got rank " +

}
return this.executeOp('batchNorm3D', function () { return _this.backend.batchNormalization3D(x, mean, variance, varianceEpsilon, scale, offset); });
return this.backendEngine.executeKernel('BatchNorm3D', { inputs: { x: x, mean: mean, variance: variance, scale: scale, offset: offset }, args: { varianceEpsilon: varianceEpsilon } });
};

@@ -973,3 +964,6 @@ NDArrayMath.prototype.multiRNNCell = function (lstmCells, data, c, h) {

return this.executeOp('multinomial', function () {
var res = _this.backend.multinomial(probabilities, numSamples, seed);
var res = _this.backendEngine.executeKernel('Multinomial', {
inputs: { probs: probabilities },
args: { numSamples: numSamples, seed: seed }
});
if (origRank === 1) {

@@ -982,3 +976,2 @@ return res.as1D();

NDArrayMath.prototype.oneHot = function (indices, depth, onValue, offValue) {
var _this = this;
if (onValue === void 0) { onValue = 1; }

@@ -989,3 +982,3 @@ if (offValue === void 0) { offValue = 0; }

}
return this.executeOp('oneHot', function () { return _this.backend.oneHot(indices, depth, onValue, offValue); });
return this.backendEngine.executeKernel('OneHot', { inputs: { indices: indices }, args: { depth: depth, onValue: onValue, offValue: offValue } });
};

@@ -1009,2 +1002,6 @@ NDArrayMath.prototype.moments = function (x, axis, keepDims) {

};
NDArrayMath.prototype.disposeData = function (id) {
this.backend.disposeData(id);
this.numArrays--;
};
return NDArrayMath;

@@ -1011,0 +1008,0 @@ }());

@@ -1,7 +0,2 @@

import { GPGPUContext } from './backends/webgl/gpgpu_context';
import { TextureType } from './backends/webgl/tex_util';
import { TextureManager } from './backends/webgl/texture_manager';
import { RandNormalDataTypes } from './rand';
export declare let GPGPU: GPGPUContext;
export declare let TEXTURE_MANAGER: TextureManager;
export declare enum DType {

@@ -18,10 +13,8 @@ float32 = "float32",

export interface NDArrayData<T extends keyof DataTypes> {
id?: number;
values?: DataTypes[T];
texture?: WebGLTexture;
textureShapeRC?: [number, number];
textureType?: TextureType;
isDisposed?: boolean;
}
export declare function initializeGPU(gpgpu: GPGPUContext, textureManager: TextureManager): void;
export declare class NDArray<T extends keyof DataTypes = keyof DataTypes> {
static nextId: number;
id: number;
shape: number[];

@@ -31,5 +24,6 @@ size: number;

protected strides: number[];
private ndarrayData;
protected constructor(shape: number[], data: NDArrayData<T>, dtype: T);
protected constructor(shape: number[], dtype: T, values?: DataTypes[T], id?: number);
static ones<T extends keyof DataTypes = keyof DataTypes>(shape: number[], dtype?: T): NDArray<T>;
static zeros<T extends keyof DataTypes = keyof DataTypes>(shape: number[], dtype?: T): NDArray<T>;
static onesLike<G extends keyof DataTypes, T extends NDArray<G>>(another: T): T;
static zerosLike<G extends keyof DataTypes, T extends NDArray<G>>(another: T): T;

@@ -55,3 +49,2 @@ static like<G extends keyof DataTypes, T extends NDArray<G>>(another: T): T;

fill(value: number): void;
getData(): NDArrayData<T>;
getValues(): DataTypes[T];

@@ -61,9 +54,3 @@ getValuesAsync(): Promise<DataTypes[T]>;

dataSync(): DataTypes[T];
private uploadToGPU();
getTexture(): WebGLTexture;
getTextureShapeRC(): [number, number];
private throwIfDisposed();
dispose(): void;
private disposeTexture();
inGPU(): boolean;
equals(t: NDArray<T>): boolean;

@@ -74,13 +61,9 @@ static rand<T extends keyof DataTypes>(shape: number[], randFunction: () => number, dtype?: T): NDArray<T>;

static randUniform<T extends keyof DataTypes>(shape: number[], a: number, b: number, dtype?: T): NDArray<T>;
private isDisposed;
private throwIfDisposed();
}
export declare class Scalar<T extends keyof DataTypes = keyof DataTypes> extends NDArray<T> {
constructor(data: NDArrayData<T>, dtype: T);
static new<T extends keyof DataTypes = keyof DataTypes>(value: number | boolean, dtype?: T): Scalar<T>;
static ZERO: Scalar<"float32" | "int32" | "bool">;
static ONE: Scalar<"float32" | "int32" | "bool">;
static TWO: Scalar<"float32" | "int32" | "bool">;
static NEG_ONE: Scalar<"float32" | "int32" | "bool">;
get(): number;
val(): Promise<number>;
set(value: number): void;
add(value: number): void;

@@ -93,6 +76,4 @@ asType<G extends keyof DataTypes>(dtype: G): Scalar<G>;

shape: [number];
constructor(data: NDArrayData<T>, dtype: T);
static new<T extends keyof DataTypes = keyof DataTypes>(values: DataTypes[T] | number[] | boolean[], dtype?: T): Array1D<T>;
get(i: number): number;
set(value: number, i: number): void;
val(i: number): Promise<number>;

@@ -103,2 +84,3 @@ add(value: number, i: number): void;

asType<G extends keyof DataTypes>(dtype: G): Array1D<G>;
static ones<T extends keyof DataTypes = keyof DataTypes>(shape: [number], dtype?: T): Array1D<T>;
static zeros<T extends keyof DataTypes = keyof DataTypes>(shape: [number], dtype?: T): Array1D<T>;

@@ -112,6 +94,5 @@ static randNormal<T extends keyof RandNormalDataTypes>(shape: [number], mean?: number, stdDev?: number, dtype?: T, seed?: number): Array1D<T>;

private stride0;
constructor(shape: [number, number], data: NDArrayData<T>, dtype: T);
constructor(shape: [number, number], dtype: T, values?: DataTypes[T], id?: number);
static new<T extends keyof DataTypes = keyof DataTypes>(shape: [number, number], values: DataTypes[T] | number[] | number[][] | boolean[] | boolean[][], dtype?: T): Array2D<T>;
get(i: number, j: number): number;
set(value: number, i: number, j: number): void;
add(value: number, i: number, j: number): void;

@@ -122,2 +103,3 @@ val(i: number, j: number): Promise<number>;

asType<G extends keyof DataTypes>(dtype: G): Array2D<G>;
static ones<T extends keyof DataTypes = keyof DataTypes>(shape: [number, number], dtype?: T): Array2D<T>;
static zeros<T extends keyof DataTypes = keyof DataTypes>(shape: [number, number], dtype?: T): Array2D<T>;

@@ -132,6 +114,5 @@ static randNormal<T extends keyof RandNormalDataTypes>(shape: [number, number], mean?: number, stdDev?: number, dtype?: T, seed?: number): Array2D<T>;

private stride1;
constructor(shape: [number, number, number], data: NDArrayData<T>, dtype: T);
constructor(shape: [number, number, number], dtype: T, values?: DataTypes[T], id?: number);
static new<T extends keyof DataTypes = keyof DataTypes>(shape: [number, number, number], values: DataTypes[T] | number[] | number[][][] | boolean[] | boolean[][][], dtype?: T): Array3D<T>;
get(i: number, j: number, k: number): number;
set(value: number, i: number, j: number, k: number): void;
val(i: number, j: number, k: number): Promise<number>;

@@ -142,2 +123,3 @@ add(value: number, i: number, j: number, k: number): void;

asType<G extends keyof DataTypes>(dtype: G): Array3D<G>;
static ones<T extends keyof DataTypes = keyof DataTypes>(shape: [number, number, number], dtype?: T): Array3D<T>;
static zeros<T extends keyof DataTypes = keyof DataTypes>(shape: [number, number, number], dtype?: T): Array3D<T>;

@@ -153,6 +135,5 @@ static randNormal<T extends keyof RandNormalDataTypes>(shape: [number, number, number], mean?: number, stdDev?: number, dtype?: T, seed?: number): Array3D<T>;

private stride2;
constructor(shape: [number, number, number, number], data: NDArrayData<T>, dtype: T);
constructor(shape: [number, number, number, number], dtype: T, values?: DataTypes[T], id?: number);
static new<T extends keyof DataTypes = keyof DataTypes>(shape: [number, number, number, number], values: DataTypes[T] | number[] | number[][][][] | boolean[] | boolean[][][][], dtype?: T): Array4D<T>;
get(i: number, j: number, k: number, l: number): number;
set(value: number, i: number, j: number, k: number, l: number): void;
val(i: number, j: number, k: number, l: number): Promise<number>;

@@ -163,2 +144,3 @@ add(value: number, i: number, j: number, k: number, l: number): void;

asType<G extends keyof DataTypes>(dtype: G): Array4D<G>;
static ones<T extends keyof DataTypes = keyof DataTypes>(shape: [number, number, number, number], dtype?: T): Array4D<T>;
static zeros<T extends keyof DataTypes = keyof DataTypes>(shape: [number, number, number, number], dtype?: T): Array4D<T>;

@@ -165,0 +147,0 @@ static randNormal<T extends keyof RandNormalDataTypes>(shape: [number, number, number, number], mean?: number, stdDev?: number, dtype?: T, seed?: number): Array4D<T>;

@@ -50,7 +50,3 @@ "use strict";

var util = require("../util");
var tex_util_1 = require("./backends/webgl/tex_util");
var webgl_util = require("./backends/webgl/webgl_util");
var rand_1 = require("./rand");
exports.GPGPU = null;
exports.TEXTURE_MANAGER = null;
var DType;

@@ -62,26 +58,11 @@ (function (DType) {

})(DType = exports.DType || (exports.DType = {}));
function initializeGPU(gpgpu, textureManager) {
exports.GPGPU = gpgpu;
exports.TEXTURE_MANAGER = textureManager;
}
exports.initializeGPU = initializeGPU;
function throwIfGPUNotInitialized() {
if (exports.GPGPU == null || exports.TEXTURE_MANAGER == null) {
throw new Error('GPU not intialized.');
}
}
var NDArray = (function () {
function NDArray(shape, data, dtype) {
util.assert(data.values != null || data.texture != null, 'Either `values` or `texture` must be defined');
util.assert(data.texture == null || (data.textureShapeRC != null), '`textureShape` must be defined when `texture` is defined');
function NDArray(shape, dtype, values, id) {
this.isDisposed = false;
this.size = util.sizeFromShape(shape);
if (data.values != null) {
util.assert(this.size === data.values.length, "Constructing ndarray of shape (" + this.size + ") should match the " +
("length of values (" + data.values.length + ")"));
if (values != null) {
util.assert(this.size === values.length, "Constructing ndarray of shape (" + this.size + ") should match the " +
("length of values (" + values.length + ")"));
}
this.shape = shape;
if (data.textureType == null) {
data.textureType = tex_util_1.TextureType.DEFAULT;
}
this.ndarrayData = data;
this.dtype = dtype || 'float32';

@@ -99,3 +80,13 @@ var dim = this.shape.length;

}
this.id = id;
if (this.id == null) {
this.id = NDArray.nextId++;
environment_1.ENV.math.register(this);
environment_1.ENV.math.write(this.id, values, this.dtype, this.shape);
}
}
NDArray.ones = function (shape, dtype) {
var values = makeOnesTypedArray(util.sizeFromShape(shape), dtype);
return NDArray.make(shape, { values: values }, dtype);
};
NDArray.zeros = function (shape, dtype) {

@@ -105,2 +96,5 @@ var values = makeZerosTypedArray(util.sizeFromShape(shape), dtype);

};
NDArray.onesLike = function (another) {
return NDArray.ones(another.shape, another.dtype);
};
NDArray.zerosLike = function (another) {

@@ -114,18 +108,15 @@ return NDArray.zeros(another.shape, another.dtype);

NDArray.make = function (shape, data, dtype) {
if (data.isDisposed) {
throw new Error("Cannot make new NDArray from disposed NDArrayData.");
}
switch (shape.length) {
case 0:
return new Scalar(data, dtype);
return new Scalar(shape, dtype, data.values, data.id);
case 1:
return new Array1D(data, dtype);
return new Array1D(shape, dtype, data.values, data.id);
case 2:
return new Array2D(shape, data, dtype);
return new Array2D(shape, dtype, data.values, data.id);
case 3:
return new Array3D(shape, data, dtype);
return new Array3D(shape, dtype, data.values, data.id);
case 4:
return new Array4D(shape, data, dtype);
return new Array4D(shape, dtype, data.values, data.id);
default:
return new NDArray(shape, data, dtype);
return new NDArray(shape, dtype, data.values, data.id);
}

@@ -138,8 +129,7 @@ };

}
var ndarrayData = {};
var shape = [pixels.height, pixels.width, numChannels];
var textureShapeRC = [shape[0], shape[1]];
var texture = exports.TEXTURE_MANAGER.acquireTexture(textureShapeRC);
var textureType = tex_util_1.TextureType.RGBA_COLOR;
exports.GPGPU.uploadPixelDataToTexture(texture, pixels);
return Array3D.make(shape, { texture: texture, textureShapeRC: textureShapeRC, textureType: textureType });
var res = NDArray.make(shape, ndarrayData, 'int32');
environment_1.ENV.math.writePixels(res.id, pixels, numChannels);
return res;
};

@@ -152,6 +142,8 @@ NDArray.prototype.reshape = function (newShape) {

}
var data = { id: this.id };
util.assert(this.size === util.sizeFromShape(newShape), 'new shape and old shape must have the same number of elements.');
return NDArray.make(newShape, this.ndarrayData, this.dtype);
return NDArray.make(newShape, data, this.dtype);
};
NDArray.prototype.flatten = function () {
this.throwIfDisposed();
if (this instanceof Array1D) {

@@ -163,2 +155,3 @@ return this;

NDArray.prototype.asScalar = function () {
this.throwIfDisposed();
util.assert(this.size === 1, 'The array must have only 1 element.');

@@ -168,11 +161,15 @@ return this.reshape([]);

NDArray.prototype.as1D = function () {
this.throwIfDisposed();
return this.reshape([this.size]);
};
NDArray.prototype.as2D = function (rows, columns) {
this.throwIfDisposed();
return this.reshape([rows, columns]);
};
NDArray.prototype.as3D = function (rows, columns, depth) {
this.throwIfDisposed();
return this.reshape([rows, columns, depth]);
};
NDArray.prototype.as4D = function (rows, columns, depth, depth2) {
this.throwIfDisposed();
return this.reshape([rows, columns, depth, depth2]);

@@ -182,11 +179,8 @@ };

this.throwIfDisposed();
var newData = this.getData();
if (newData.values != null) {
newData = { values: toTypedArray(newData.values, dtype) };
}
return NDArray.make(this.shape, newData, dtype);
var vals = this.dataSync();
var newVals = toTypedArray(vals, dtype);
return NDArray.make(this.shape, { values: newVals }, dtype);
};
Object.defineProperty(NDArray.prototype, "rank", {
get: function () {
this.throwIfDisposed();
return this.shape.length;

@@ -202,3 +196,2 @@ },

}
this.throwIfDisposed();
var index = locs[locs.length - 1];

@@ -215,3 +208,2 @@ for (var i = 0; i < locs.length - 1; ++i) {

}
this.throwIfDisposed();
this.set.apply(this, [this.get.apply(this, locs) + value].concat(locs));

@@ -225,7 +217,12 @@ };

this.throwIfDisposed();
var index = locs[locs.length - 1];
util.assert(locs.length === this.rank, "The number of provided coordinates (" + locs.length + ") must " +
("match the rank (" + this.rank + ")"));
var index = locs.length > 0 ? locs[locs.length - 1] : 0;
for (var i = 0; i < locs.length - 1; ++i) {
index += this.strides[i] * locs[i];
}
this.getValues()[index] = value;
var vals = this.getValues();
vals[index] = value;
environment_1.ENV.math.disposeData(this.id);
environment_1.ENV.math.write(this.id, vals, this.dtype, this.shape);
};

@@ -251,2 +248,3 @@ NDArray.prototype.val = function () {

NDArray.prototype.locToIndex = function (locs) {
this.throwIfDisposed();
var index = locs[locs.length - 1];

@@ -259,2 +257,3 @@ for (var i = 0; i < locs.length - 1; ++i) {

NDArray.prototype.indexToLoc = function (index) {
this.throwIfDisposed();
var locs = new Array(this.shape.length);

@@ -270,7 +269,7 @@ for (var i = 0; i < locs.length - 1; ++i) {

this.throwIfDisposed();
this.getValues().fill(value);
var vals = this.getValues();
vals.fill(value);
environment_1.ENV.math.disposeData(this.id);
environment_1.ENV.math.write(this.id, vals, this.dtype, this.shape);
};
NDArray.prototype.getData = function () {
return this.ndarrayData;
};
NDArray.prototype.getValues = function () {

@@ -284,28 +283,5 @@ return this.dataSync();

return __awaiter(this, void 0, void 0, function () {
var _a, queryFn;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
this.throwIfDisposed();
if (this.ndarrayData.values != null) {
return [2, this.ndarrayData.values];
}
if (!(environment_1.ENV.get('WEBGL_GET_BUFFER_SUB_DATA_ASYNC_EXTENSION_ENABLED') &&
this.ndarrayData.textureType === tex_util_1.TextureType.DEFAULT)) return [3, 2];
_a = this.ndarrayData;
return [4, exports.GPGPU.downloadMatrixFromTextureAsync(this.ndarrayData.texture, this.ndarrayData.textureShapeRC[0], this.ndarrayData.textureShapeRC[1])];
case 1:
_a.values = _b.sent();
return [2, this.ndarrayData.values];
case 2:
if (!!environment_1.ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_ENABLED')) return [3, 4];
return [4, this.dataSync()];
case 3: return [2, _b.sent()];
case 4:
queryFn = function () { };
return [4, exports.GPGPU.runQuery(queryFn)];
case 5:
_b.sent();
return [2, this.dataSync()];
}
return __generator(this, function (_a) {
this.throwIfDisposed();
return [2, environment_1.ENV.math.read(this.id)];
});

@@ -316,65 +292,8 @@ });

this.throwIfDisposed();
if (this.ndarrayData.values == null) {
throwIfGPUNotInitialized();
var values = void 0;
if (this.ndarrayData.textureType === tex_util_1.TextureType.DEFAULT) {
values = exports.GPGPU.downloadMatrixFromTexture(this.ndarrayData.texture, this.ndarrayData.textureShapeRC[0], this.ndarrayData.textureShapeRC[1]);
}
else {
values = exports.GPGPU.downloadMatrixFromRGBAColorTexture(this.ndarrayData.texture, this.ndarrayData.textureShapeRC[0], this.ndarrayData.textureShapeRC[1], this.shape[2]);
}
this.ndarrayData.values = float32ToTypedArray(values, this.dtype);
this.disposeTexture();
}
return this.ndarrayData.values;
return environment_1.ENV.math.readSync(this.id);
};
NDArray.prototype.uploadToGPU = function () {
throwIfGPUNotInitialized();
this.throwIfDisposed();
this.ndarrayData.textureShapeRC =
webgl_util.getTextureShapeFromLogicalShape(exports.GPGPU.gl, this.shape);
this.ndarrayData.texture =
exports.TEXTURE_MANAGER.acquireTexture(this.ndarrayData.textureShapeRC);
this.ndarrayData.textureType = tex_util_1.TextureType.DEFAULT;
exports.GPGPU.uploadMatrixToTexture(this.ndarrayData.texture, this.ndarrayData.textureShapeRC[0], this.ndarrayData.textureShapeRC[1], typedArrayToFloat32(this.ndarrayData.values, this.dtype));
this.ndarrayData.values = null;
};
NDArray.prototype.getTexture = function () {
this.throwIfDisposed();
if (this.ndarrayData.texture == null) {
this.uploadToGPU();
}
return this.ndarrayData.texture;
};
NDArray.prototype.getTextureShapeRC = function () {
this.throwIfDisposed();
if (this.ndarrayData.textureShapeRC == null) {
this.uploadToGPU();
}
return this.ndarrayData.textureShapeRC;
};
NDArray.prototype.throwIfDisposed = function () {
if (this.ndarrayData.isDisposed) {
throw new Error("NDArray is disposed.");
}
};
NDArray.prototype.dispose = function () {
this.ndarrayData.values = null;
this.shape = null;
this.ndarrayData.isDisposed = true;
if (this.ndarrayData.texture != null) {
this.disposeTexture();
}
this.isDisposed = true;
environment_1.ENV.math.disposeData(this.id);
};
NDArray.prototype.disposeTexture = function () {
throwIfGPUNotInitialized();
exports.TEXTURE_MANAGER.releaseTexture(this.ndarrayData.texture, this.ndarrayData.textureShapeRC);
this.ndarrayData.texture = null;
this.ndarrayData.textureShapeRC = null;
this.ndarrayData.textureType = null;
};
NDArray.prototype.inGPU = function () {
this.throwIfDisposed();
return this.ndarrayData.texture != null;
};
NDArray.prototype.equals = function (t) {

@@ -426,2 +345,8 @@ this.throwIfDisposed();

};
NDArray.prototype.throwIfDisposed = function () {
if (this.isDisposed) {
throw new Error("NDArray is disposed.");
}
};
NDArray.nextId = 0;
return NDArray;

@@ -432,13 +357,8 @@ }());

__extends(Scalar, _super);
function Scalar(data, dtype) {
var _this = this;
if (data.texture != null) {
data.textureShapeRC = [1, 1];
}
_this = _super.call(this, [], data, dtype) || this;
return _this;
function Scalar() {
return _super !== null && _super.apply(this, arguments) || this;
}
Scalar.new = function (value, dtype) {
var values = [value];
return new Scalar({ values: toTypedArray(values, dtype) }, dtype);
return new Scalar([], dtype, toTypedArray(values, dtype));
};

@@ -460,5 +380,2 @@ Scalar.prototype.get = function () {

};
Scalar.prototype.set = function (value) {
this.getValues()[0] = value;
};
Scalar.prototype.add = function (value) {

@@ -476,6 +393,2 @@ this.getValues()[0] += value;

};
Scalar.ZERO = Scalar.new(0);
Scalar.ONE = Scalar.new(1);
Scalar.TWO = Scalar.new(2);
Scalar.NEG_ONE = Scalar.new(-1);
return Scalar;

@@ -486,9 +399,4 @@ }(NDArray));

__extends(Array1D, _super);
function Array1D(data, dtype) {
var _this = this;
var shape = (data.values != null) ?
[data.values.length] :
[util.sizeFromShape(data.textureShapeRC)];
_this = _super.call(this, shape, data, dtype) || this;
return _this;
function Array1D() {
return _super !== null && _super.apply(this, arguments) || this;
}

@@ -501,3 +409,3 @@ Array1D.new = function (values, dtype) {

}
return new Array1D({ values: toTypedArray(values, dtype) }, dtype);
return new Array1D([values.length], dtype, toTypedArray(values, dtype));
};

@@ -507,5 +415,2 @@ Array1D.prototype.get = function (i) {

};
Array1D.prototype.set = function (value, i) {
this.getValues()[i] = value;
};
Array1D.prototype.val = function (i) {

@@ -535,2 +440,5 @@ return __awaiter(this, void 0, void 0, function () {

};
Array1D.ones = function (shape, dtype) {
return NDArray.ones(shape, dtype);
};
Array1D.zeros = function (shape, dtype) {

@@ -565,6 +473,6 @@ return NDArray.zeros(shape, dtype);

__extends(Array2D, _super);
function Array2D(shape, data, dtype) {
function Array2D(shape, dtype, values, id) {
var _this = this;
util.assert(shape.length === 2, 'Shape should be of length 2');
_this = _super.call(this, shape, data, dtype) || this;
_this = _super.call(this, shape, dtype, values, id) || this;
_this.stride0 = _this.strides[0];

@@ -582,3 +490,3 @@ return _this;

}
return new Array2D(shape, { values: toTypedArray(values, dtype) }, dtype);
return new Array2D(shape, dtype, toTypedArray(values, dtype));
};

@@ -588,5 +496,2 @@ Array2D.prototype.get = function (i, j) {

};
Array2D.prototype.set = function (value, i, j) {
this.getValues()[this.stride0 * i + j] = value;
};
Array2D.prototype.add = function (value, i, j) {

@@ -616,2 +521,5 @@ this.getValues()[this.stride0 * i + j] += value;

};
Array2D.ones = function (shape, dtype) {
return NDArray.ones(shape, dtype);
};
Array2D.zeros = function (shape, dtype) {

@@ -646,6 +554,6 @@ return NDArray.zeros(shape, dtype);

__extends(Array3D, _super);
function Array3D(shape, data, dtype) {
function Array3D(shape, dtype, values, id) {
var _this = this;
util.assert(shape.length === 3, 'Shape should be of length 3');
_this = _super.call(this, shape, data, dtype) || this;
_this = _super.call(this, shape, dtype, values, id) || this;
_this.stride0 = _this.strides[0];

@@ -664,3 +572,3 @@ _this.stride1 = _this.strides[1];

}
return new Array3D(shape, { values: toTypedArray(values, dtype) }, dtype);
return new Array3D(shape, dtype, toTypedArray(values, dtype));
};

@@ -670,5 +578,2 @@ Array3D.prototype.get = function (i, j, k) {

};
Array3D.prototype.set = function (value, i, j, k) {
this.getValues()[this.stride0 * i + this.stride1 * j + k] = value;
};
Array3D.prototype.val = function (i, j, k) {

@@ -700,2 +605,5 @@ return __awaiter(this, void 0, void 0, function () {

};
Array3D.ones = function (shape, dtype) {
return NDArray.ones(shape, dtype);
};
Array3D.zeros = function (shape, dtype) {

@@ -730,6 +638,6 @@ return NDArray.zeros(shape, dtype);

__extends(Array4D, _super);
function Array4D(shape, data, dtype) {
function Array4D(shape, dtype, values, id) {
var _this = this;
util.assert(shape.length === 4, 'Shape should be of length 4');
_this = _super.call(this, shape, data, dtype) || this;
_this = _super.call(this, shape, dtype, values, id) || this;
_this.stride0 = _this.strides[0];

@@ -749,3 +657,3 @@ _this.stride1 = _this.strides[1];

}
return new Array4D(shape, { values: toTypedArray(values, dtype) }, dtype);
return new Array4D(shape, dtype, toTypedArray(values, dtype));
};

@@ -755,5 +663,2 @@ Array4D.prototype.get = function (i, j, k, l) {

};
Array4D.prototype.set = function (value, i, j, k, l) {
this.getValues()[this.stride0 * i + this.stride1 * j + this.stride2 * k + l] = value;
};
Array4D.prototype.val = function (i, j, k, l) {

@@ -788,2 +693,5 @@ return __awaiter(this, void 0, void 0, function () {

};
Array4D.ones = function (shape, dtype) {
return NDArray.ones(shape, dtype);
};
Array4D.zeros = function (shape, dtype) {

@@ -821,3 +729,13 @@ return NDArray.zeros(shape, dtype);

else if (dtype === 'int32') {
return new Int32Array(array);
var vals = new Int32Array(array.length);
for (var i = 0; i < vals.length; ++i) {
var val = array[i];
if (util.isValNaN(val, 'int32')) {
vals[i] = util.getNaN('int32');
}
else {
vals[i] = val;
}
}
return vals;
}

@@ -831,3 +749,3 @@ else if (dtype === 'bool') {

}
else if (val) {
else if (Math.round(val) !== 0) {
bool[i] = 1;

@@ -874,32 +792,8 @@ }

}
function typedArrayToFloat32(a, dtype) {
if (a instanceof Float32Array) {
return a;
function makeOnesTypedArray(size, dtype) {
var array = makeZerosTypedArray(size, dtype);
for (var i = 0; i < array.length; i++) {
array[i] = 1;
}
else {
var res = new Float32Array(a.length);
for (var i = 0; i < res.length; i++) {
var val = a[i];
res[i] = util.isValNaN(val, dtype) ? NaN : val;
}
return res;
}
return array;
}
function float32ToTypedArray(a, dtype) {
if (dtype === 'float32') {
return a;
}
else if (dtype === 'int32' || dtype === 'bool') {
var result = (dtype === 'int32') ? new Int32Array(a.length) :
new Uint8Array(a.length);
for (var i = 0; i < result.length; ++i) {
var val = a[i];
val = isNaN(val) ? util.getNaN(dtype) : Math.round(val);
result[i] = val;
}
return result;
}
else {
throw new Error("Unknown dtype " + dtype);
}
}

@@ -12,4 +12,4 @@ "use strict";

if (this.truncated) {
this.upper = this.mean + Math.pow(this.stdDev, 2);
this.lower = this.mean - Math.pow(this.stdDev, 2);
this.upper = this.mean + this.stdDev * 2;
this.lower = this.mean - this.stdDev * 2;
}

@@ -16,0 +16,0 @@ var seedValue = seed ? seed : Math.random();

@@ -0,1 +1,2 @@

import { DType } from '../util';
export interface SumTypes {

@@ -11,1 +12,37 @@ float32: 'float32';

}
export interface UpcastInt32And {
float32: 'float32';
int32: 'int32';
bool: 'int32';
}
export declare enum UpcastInt32AndMap {
float32 = "float32",
int32 = "int32",
bool = "int32",
}
export interface UpcastBoolAnd {
float32: 'float32';
int32: 'int32';
bool: 'bool';
}
export declare enum UpcastBoolAndMap {
float32 = "float32",
int32 = "int32",
bool = "bool",
}
export interface UpcastFloat32And {
float32: 'float32';
int32: 'float32';
bool: 'float32';
}
export declare enum UpcastFloat32AndMap {
float32 = "float32",
int32 = "float32",
bool = "float32",
}
export interface UpcastType {
float32: UpcastFloat32And;
int32: UpcastInt32And;
bool: UpcastBoolAnd;
}
export declare function upcastType(typeA: DType, typeB: DType): DType;

@@ -9,1 +9,28 @@ "use strict";

})(SumTypesMap = exports.SumTypesMap || (exports.SumTypesMap = {}));
var UpcastInt32AndMap;
(function (UpcastInt32AndMap) {
UpcastInt32AndMap["float32"] = "float32";
UpcastInt32AndMap["int32"] = "int32";
UpcastInt32AndMap["bool"] = "int32";
})(UpcastInt32AndMap = exports.UpcastInt32AndMap || (exports.UpcastInt32AndMap = {}));
var UpcastBoolAndMap;
(function (UpcastBoolAndMap) {
UpcastBoolAndMap["float32"] = "float32";
UpcastBoolAndMap["int32"] = "int32";
UpcastBoolAndMap["bool"] = "bool";
})(UpcastBoolAndMap = exports.UpcastBoolAndMap || (exports.UpcastBoolAndMap = {}));
var UpcastFloat32AndMap;
(function (UpcastFloat32AndMap) {
UpcastFloat32AndMap["float32"] = "float32";
UpcastFloat32AndMap["int32"] = "float32";
UpcastFloat32AndMap["bool"] = "float32";
})(UpcastFloat32AndMap = exports.UpcastFloat32AndMap || (exports.UpcastFloat32AndMap = {}));
var upcastTypeMap = {
float32: UpcastFloat32AndMap,
int32: UpcastInt32AndMap,
bool: UpcastBoolAndMap
};
function upcastType(typeA, typeB) {
return upcastTypeMap[typeA][typeB];
}
exports.upcastType = upcastType;

@@ -7,2 +7,3 @@ "use strict";

var backend_webgl_1 = require("./math/backends/backend_webgl");
var math_1 = require("./math/math");
var util = require("./util");

@@ -163,3 +164,6 @@ exports.TEST_EPSILON = 1e-2;

var testNameBase = 'CPU: math.' + name;
describeWithFeaturesAndExecutor(testNameBase, tests, function (testName, tests, features) { return executeMathTests(testName, tests, function () { return new backend_cpu_1.NDArrayMathCPU(); }, features); }, featuresList);
describeWithFeaturesAndExecutor(testNameBase, tests, function (testName, tests, features) { return executeMathTests(testName, tests, function () {
var safeMode = true;
return new math_1.NDArrayMath(new backend_cpu_1.MathBackendCPU(), safeMode);
}, features); }, featuresList);
}

@@ -169,3 +173,6 @@ exports.describeMathCPU = describeMathCPU;

var testNameBase = 'WebGL: math.' + name;
describeWithFeaturesAndExecutor(testNameBase, tests, function (testName, tests, features) { return executeMathTests(testName, tests, function () { return new backend_webgl_1.NDArrayMathGPU(); }, features); }, featuresList);
describeWithFeaturesAndExecutor(testNameBase, tests, function (testName, tests, features) { return executeMathTests(testName, tests, function () {
var safeMode = true;
return new math_1.NDArrayMath(new backend_webgl_1.MathBackendWebGL(), safeMode);
}, features); }, featuresList);
}

@@ -204,3 +211,5 @@ exports.describeMathGPU = describeMathGPU;

var math;
var oldMath;
var customBeforeEach = function () {
oldMath = environment_1.ENV.math;
math = mathFactory();

@@ -212,2 +221,3 @@ math.startScope();

math.dispose();
environment_1.ENV.setGlobalMath(oldMath);
};

@@ -214,0 +224,0 @@ var customIt = function (name, testFunc) {

@@ -227,4 +227,7 @@ "use strict";

function isValNaN(val, dtype) {
if (isNaN(val)) {
return true;
}
if (dtype === 'float32') {
return isNaN(val);
return false;
}

@@ -231,0 +234,0 @@ else if (dtype === 'int32') {

@@ -1,2 +0,2 @@

declare const version = "0.3.13";
declare const version = "0.3.14";
export { version };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var version = '0.3.13';
var version = '0.3.14';
exports.version = version;
{
"name": "deeplearn",
"version": "0.3.13",
"version": "0.3.14",
"description": "Hardware-accelerated JavaScript library for machine intelligence",
"private": false,
"main": "dist/index.js",
"jsdelivr": "dist/deeplearn.js",
"unpkg": "dist/deeplearn.js",

@@ -8,0 +9,0 @@ "types": "dist/index.d.ts",

@@ -51,5 +51,7 @@ <a id="travis-badge" href="https://travis-ci.org/PAIR-code/deeplearnjs" alt="Build Status">

You can also use **deeplearn.js** with plain JavaScript. Load the latest version
of the library from [unpkg](https://unpkg.com):
of the library from [jsDelivr](https://www.jsdelivr.com/) or [unpkg](https://unpkg.com):
```html
<script src="https://cdn.jsdelivr.net/npm/deeplearn"></script>
<!-- or -->
<script src="https://unpkg.com/deeplearn"></script>

@@ -91,4 +93,5 @@ ```

We recommend using [Visual Studio Code](https://code.visualstudio.com/) for
development. Make sure to install [TSLint VSCode extension](https://marketplace.visualstudio.com/items?itemName=eg2.tslint) and the `clang-format` command line tool with the [Clang-Format VSCode extension](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format) for auto-formatting.
development. Make sure to install [TSLint VSCode extension](https://marketplace.visualstudio.com/items?itemName=eg2.tslint) and the `clang-format` command line tool with the [Clang-Format VSCode extension](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format) for auto-formatting (Be sure to use the [NPM/Yarn clang-format binary](https://github.com/angular/clang-format)).
To interactively develop any of the demos (e.g. `demos/nn-art/`):

@@ -95,0 +98,0 @@

{
"extends": ["tslint-no-circular-imports"],
"rules": {

@@ -4,0 +3,0 @@ "array-type": [true, "array-simple"],

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc