Skip to content
This repository was archived by the owner on Aug 15, 2019. It is now read-only.

Add more math operations. Highlight error lines in webgl shaders #123

Merged
merged 6 commits into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 81 additions & 8 deletions src/math/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,15 @@ export abstract class NDArrayMath {
}
protected abstract sqrtInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes absolute value element-wise.
* @param ndarray The input NDArray.
*/
abs<T extends NDArray>(ndarray: T): T {
return this.executeOp('abs', () => this.absInternal(ndarray));
}
protected abstract absInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes rectified linear element-wise, max(x, 0).
* @param ndarray The input NDArray.
Expand All @@ -809,22 +818,86 @@ export abstract class NDArrayMath {
protected abstract sigmoidInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes hyperbolic tangent of the input NDArray element-wise.
* Computes sin of the input NDArray element-wise, y = sin(x).
* @param ndarray The input NDArray.
*/
tanh<T extends NDArray>(ndarray: T): T {
return this.executeOp('tanh', () => this.tanhInternal(ndarray));
sin<T extends NDArray>(ndarray: T): T {
return this.executeOp('sin', () => this.sinInternal(ndarray));
}
protected abstract tanhInternal<T extends NDArray>(ndarray: T): T;
protected abstract sinInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes sin of the input NDArray element-wise, y = sin(x).
* Computes cos of the input NDArray element-wise, y = cos(x).
* @param ndarray The input NDArray.
*/
sin<T extends NDArray>(ndarray: T): T {
return this.executeOp('sin', () => this.sinInternal(ndarray));
cos<T extends NDArray>(ndarray: T): T {
return this.executeOp('cos', () => this.cosInternal(ndarray));
}
protected abstract sinInternal<T extends NDArray>(ndarray: T): T;
protected abstract cosInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes tan of the input NDArray element-wise, y = tan(x).
* @param ndarray The input NDArray.
*/
tan<T extends NDArray>(ndarray: T): T {
return this.executeOp('tan', () => this.tanInternal(ndarray));
}
protected abstract tanInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes asin of the input NDArray element-wise, y = asin(x).
* @param ndarray The input NDArray.
*/
asin<T extends NDArray>(ndarray: T): T {
return this.executeOp('asin', () => this.asinInternal(ndarray));
}
protected abstract asinInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes acos of the input NDArray element-wise, y = acos(x).
* @param ndarray The input NDArray.
*/
acos<T extends NDArray>(ndarray: T): T {
return this.executeOp('acos', () => this.acosInternal(ndarray));
}
protected abstract acosInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes atan of the input NDArray element-wise, y = atan(x).
* @param ndarray The input NDArray.
*/
atan<T extends NDArray>(ndarray: T): T {
return this.executeOp('atan', () => this.atanInternal(ndarray));
}
protected abstract atanInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes hyperbolic sin of the input NDArray element-wise, y = sinh(x).
* @param ndarray The input NDArray.
*/
sinh<T extends NDArray>(ndarray: T): T {
return this.executeOp('sinh', () => this.sinhInternal(ndarray));
}
protected abstract sinhInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes hyperbolic cos of the input NDArray element-wise, y = cosh(x).
* @param ndarray The input NDArray.
*/
cosh<T extends NDArray>(ndarray: T): T {
return this.executeOp('cosh', () => this.coshInternal(ndarray));
}
protected abstract coshInternal<T extends NDArray>(ndarray: T): T;

/**
* Computes hyperbolic tangent of the input NDArray element-wise.
* @param ndarray The input NDArray.
*/
tanh<T extends NDArray>(ndarray: T): T {
return this.executeOp('tanh', () => this.tanhInternal(ndarray));
}
protected abstract tanhInternal<T extends NDArray>(ndarray: T): T;


/**
* Computes step of the input NDArray element-wise, y = 1 if x > 0 | 0 if x <=
Expand Down
80 changes: 76 additions & 4 deletions src/math/math_cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,20 +337,20 @@ export class NDArrayMathCPU extends NDArrayMath {
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected sigmoidInternal<T extends NDArray>(ndarray: T): T {
protected absInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = 1 / (1 + Math.exp(-values[i]));
resultValues[i] = Math.abs(values[i]);
}
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected tanhInternal<T extends NDArray>(ndarray: T): T {
protected sigmoidInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = util.tanh(values[i]);
resultValues[i] = 1 / (1 + Math.exp(-values[i]));
}
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}
Expand All @@ -364,6 +364,78 @@ export class NDArrayMathCPU extends NDArrayMath {
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected cosInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = Math.cos(values[i]);
}
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected tanInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = Math.tan(values[i]);
}
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected asinInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = Math.asin(values[i]);
}
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected acosInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = Math.acos(values[i]);
}
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected atanInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = Math.atan(values[i]);
}
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected sinhInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = Math.sinh(values[i]);
}
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected coshInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = Math.cosh(values[i]);
}
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected tanhInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = util.tanh(values[i]);
}
return NDArray.make<T>(ndarray.shape, {values: resultValues});
}

protected stepInternal<T extends NDArray>(ndarray: T): T {
const resultValues = new Float32Array(ndarray.size);
const values = ndarray.getValues();
Expand Down
75 changes: 59 additions & 16 deletions src/math/math_gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {AddScaledMatProgram} from './webgl/addscaledmat_gpu';
import {ArgMaxEqualsProgram} from './webgl/argmaxequals_gpu';
import {ArgMinMaxProgram} from './webgl/argminmax_gpu';
import {BatchNormProgram} from './webgl/batchnorm_gpu';
import * as binaryop_gpu from './webgl/binaryop_gpu';
import {BinaryOpProgram} from './webgl/binaryop_gpu';
import {Concat3DProgram} from './webgl/concat3d_gpu';
// tslint:disable-next-line:max-line-length
Expand All @@ -41,7 +42,8 @@ import {Pool2DProgram} from './webgl/pool_gpu';
import {ReduceSumProgram} from './webgl/reducesum_gpu';
import {ResizeBilinear3DProgram} from './webgl/resize_bilinear_gpu';
import {TextureManager} from './webgl/texture_manager';
import {UnaryOp, UnaryOpProgram} from './webgl/unaryop_gpu';
import * as unary_op from './webgl/unaryop_gpu';
import {UnaryOpProgram} from './webgl/unaryop_gpu';
import * as webgl_util from './webgl/webgl_util';

export class NDArrayMathGPU extends NDArrayMath {
Expand Down Expand Up @@ -116,7 +118,7 @@ export class NDArrayMathGPU extends NDArrayMath {
}

protected negInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, UnaryOp.NEG);
const program = new UnaryOpProgram(a.shape, unary_op.NEG);
return this.compileAndRun<T, T>(program, [a]);
}

Expand Down Expand Up @@ -150,7 +152,7 @@ export class NDArrayMathGPU extends NDArrayMath {
}

protected multiplyInternal<T extends NDArray>(a: T, b: T): T {
const program = new BinaryOpProgram('*', a.shape, b.shape);
const program = new BinaryOpProgram(binaryop_gpu.MUL, a.shape, b.shape);
return this.compileAndRun<T, T>(program, [a, b]);
}

Expand Down Expand Up @@ -222,17 +224,17 @@ export class NDArrayMathGPU extends NDArrayMath {
}

protected divideInternal<T extends NDArray>(a: T, b: T): T {
const program = new BinaryOpProgram('/', a.shape, b.shape);
const program = new BinaryOpProgram(binaryop_gpu.DIV, a.shape, b.shape);
return this.compileAndRun<NDArray, T>(program, [a, b]);
}

protected addInternal<T extends NDArray>(a: T, b: T): T {
const program = new BinaryOpProgram('+', a.shape, b.shape);
const program = new BinaryOpProgram(binaryop_gpu.ADD, a.shape, b.shape);
return this.compileAndRun<NDArray, T>(program, [a, b]);
}

protected subInternal<T extends NDArray>(a: T, b: T): T {
const program = new BinaryOpProgram('-', a.shape, b.shape);
const program = new BinaryOpProgram(binaryop_gpu.SUB, a.shape, b.shape);
return this.compileAndRun<NDArray, T>(program, [a, b]);
}

Expand All @@ -242,42 +244,83 @@ export class NDArrayMathGPU extends NDArrayMath {
}

protected expInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, UnaryOp.EXP);
const program = new UnaryOpProgram(a.shape, unary_op.EXP);
return this.compileAndRun(program, [a]);
}

protected logInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, UnaryOp.LOG);
const program = new UnaryOpProgram(a.shape, unary_op.LOG);
return this.compileAndRun(program, [a]);
}

protected sqrtInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, UnaryOp.SQRT);
const program = new UnaryOpProgram(a.shape, unary_op.SQRT);
return this.compileAndRun(program, [a]);
}

protected reluInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, UnaryOp.RELU);
const program = new UnaryOpProgram(a.shape, unary_op.RELU);
return this.compileAndRun(program, [a]);
}

protected absInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, unary_op.ABS);
return this.compileAndRun(program, [a]);
}

protected sigmoidInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, UnaryOp.SIGMOID);
const program = new UnaryOpProgram(a.shape, unary_op.SIGMOID);
return this.compileAndRun<T, T>(program, [a]);
}

protected tanhInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, UnaryOp.TANH);
protected sinInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, unary_op.SIN);
return this.compileAndRun(program, [a]);
}

protected sinInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, UnaryOp.SIN);
protected cosInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, unary_op.COS);
return this.compileAndRun(program, [a]);
}

protected tanInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, unary_op.TAN);
return this.compileAndRun(program, [a]);
}

protected asinInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, unary_op.ASIN);
return this.compileAndRun(program, [a]);
}

protected acosInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, unary_op.ACOS);
return this.compileAndRun(program, [a]);
}

protected atanInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, unary_op.ATAN);
return this.compileAndRun(program, [a]);
}

protected sinhInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, unary_op.SINH);
return this.compileAndRun(program, [a]);
}

protected coshInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, unary_op.COSH);
return this.compileAndRun(program, [a]);
}

protected tanhInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, unary_op.TANH);
return this.compileAndRun(program, [a]);
}


protected stepInternal<T extends NDArray>(a: T): T {
const program = new UnaryOpProgram(a.shape, UnaryOp.STEP);
const program = new UnaryOpProgram(a.shape, unary_op.STEP);
return this.compileAndRun(program, [a]);
}

Expand Down
Loading