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

Fix demos and models #461

Merged
merged 9 commits into from
Dec 23, 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ See the [TypeScript starter project](https://github.com/PAIR-code/deeplearnjs/tr
short example that sums an array with a scalar (broadcasted):

```ts
import {Array1D, NDArrayMathGPU, Scalar} from 'deeplearn';
import {Array1D, ENV, Scalar} from 'deeplearn';

const math = new NDArrayMathGPU();
const math = ENV.math;
const a = Array1D.new([1, 2, 3]);
const b = Scalar.new(2);

Expand Down Expand Up @@ -63,7 +63,7 @@ After importing the library, the API will be available as `dl` in the global
namespace.

```js
var math = new dl.NDArrayMathGPU();
var math = dl.ENV.math;
var a = dl.Array1D.new([1, 2, 3]);
var b = dl.Scalar.new(2);

Expand Down
4 changes: 2 additions & 2 deletions demos/adder/adder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* =============================================================================
*/

import {NDArrayMathGPU, Scalar} from 'deeplearn';
import {ENV, Scalar} from 'deeplearn';

const outputElement = document.getElementById('output');
const inA: HTMLInputElement = document.getElementById('A') as HTMLInputElement;
const inB: HTMLInputElement = document.getElementById('B') as HTMLInputElement;

const math = new NDArrayMathGPU();
const math = ENV.math;

export async function execute(event?: Event) {
const a = Scalar.new(+inA.value);
Expand Down
10 changes: 7 additions & 3 deletions demos/benchmarks/batchnormalization3d_benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* =============================================================================
*/
// tslint:disable-next-line:max-line-length
import {Array1D, Array3D, NDArrayMathCPU, NDArrayMathGPU} from 'deeplearn';
import {Array1D, Array3D, ENV, NDArrayMath} from 'deeplearn';

import {BenchmarkTest, LAST_RUN_CPU_CUTOFF_MS} from './benchmark';
import * as benchmark_util from './benchmark_util';
Expand All @@ -29,7 +29,9 @@ export class BatchNormalization3DCPUBenchmark implements BenchmarkTest {
resolve(-1);
});
}
const math = new NDArrayMathCPU();
const safeMode = false;
const math = new NDArrayMath('cpu', safeMode);
ENV.setMath(math);
const x = Array3D.randUniform([size, size, 8], -1, 1);
const mean = Array1D.new([0]);
const variance = Array1D.new([1]);
Expand All @@ -48,7 +50,9 @@ export class BatchNormalization3DCPUBenchmark implements BenchmarkTest {

export class BatchNormalization3DGPUBenchmark implements BenchmarkTest {
async run(size: number) {
const math = new NDArrayMathGPU();
const safeMode = false;
const math = new NDArrayMath('webgl', safeMode);
ENV.setMath(math);
const x = Array3D.randUniform([size, size, 8], -1, 1);
const mean = Array1D.new([0]);
const variance = Array1D.new([1]);
Expand Down
35 changes: 6 additions & 29 deletions demos/benchmarks/benchmark_util.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@

import {ENV, NDArray, NDArrayMathGPU} from 'deeplearn';
import {NDArray, NDArrayMath} from 'deeplearn';

export async function warmupAndBenchmarkGPU(
math: NDArrayMathGPU, benchmark: () => NDArray): Promise<number> {
const gpgpu = math.getGPGPUContext();

let out: NDArray;
const saveOutputBenchmark = () => {
out = benchmark();
};

math: NDArrayMath, benchmark: () => NDArray): Promise<number> {
// Warmup.
if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_ENABLED')) {
await gpgpu.runQuery(saveOutputBenchmark);
} else {
saveOutputBenchmark();
out.dataSync();
}
out.dispose();

let totalTime: number;
if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE')) {
totalTime = await gpgpu.runQuery(saveOutputBenchmark);
} else {
const start = performance.now();

saveOutputBenchmark();
out.dataSync();

totalTime = performance.now() - start;
}
const out = benchmark();
await out.data();
out.dispose();
return totalTime;
// Real timing.
return math.time(benchmark);
}
58 changes: 16 additions & 42 deletions demos/benchmarks/benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class MathBenchmark extends MathBenchmarkPolymer {
return runNumberRowElement;
}

private runBenchmarkSteps(
private async runBenchmarkSteps(
chart: Chart, runGroup: BenchmarkRunGroup, benchmarkRunGroupIndex: number,
step: number) {
const runNumbersTable =
Expand Down Expand Up @@ -207,7 +207,6 @@ export class MathBenchmark extends MathBenchmarkPolymer {
const runNumberRowElement = document.createElement('div');
runNumberRowElement.className = 'run-numbers-row math-benchmark';

const runPromises: Array<Promise<number>> = [];
const rowValues: string[] = [step.toString()];
for (let i = 0; i < runGroup.benchmarkRuns.length; i++) {
const run = runGroup.benchmarkRuns[i];
Expand All @@ -218,48 +217,23 @@ export class MathBenchmark extends MathBenchmarkPolymer {
step;

const opType = runGroup.selectedOption;
runPromises.push(test.run(size, opType, runGroup.params[opType]));
}

Promise.all(runPromises).then(results => {
for (let i = 0; i < runGroup.benchmarkRuns.length; i++) {
const benchmarkRun = runGroup.benchmarkRuns[i];

const size = runGroup.stepToSizeTransformation != null ?
runGroup.stepToSizeTransformation(step) :
step;

let resultString: string;
let logString: string;
let time = 0;
let success = true;
try {
time = results[i];
resultString = time.toFixed(3) + 'ms';
logString = resultString;
} catch (e) {
success = false;
resultString = 'Error';
logString = e.message;
}
const time = await test.run(size, opType, runGroup.params[opType]);
const resultString = time.toFixed(3) + 'ms';

if (time >= 0) {
if (success) {
benchmarkRun.chartData.push({x: step, y: time});
}
rowValues.push(resultString);
}
console.log(`${benchmarkRun.name}[${size}]: ${logString}`);
if (time >= 0) {
run.chartData.push({x: step, y: time});
rowValues.push(resultString);
}
runNumbersTable.appendChild(this.buildRunNumbersRow(rowValues));

step += runGroup.stepSize;
// Allow the UI to update.
setTimeout(
() => this.runBenchmarkSteps(
chart, runGroup, benchmarkRunGroupIndex, step),
100);
});
console.log(`${run.name}[${size}]: ${resultString}`);
}
runNumbersTable.appendChild(this.buildRunNumbersRow(rowValues));

step += runGroup.stepSize;
// Allow the UI to update.
setTimeout(
() => this.runBenchmarkSteps(
chart, runGroup, benchmarkRunGroupIndex, step),
100);
}
}
document.registerElement(MathBenchmark.prototype.is, MathBenchmark);
4 changes: 2 additions & 2 deletions demos/benchmarks/conv_benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

// tslint:disable-next-line:max-line-length
import {Array1D, Array3D, Array4D, conv_util, NDArray, NDArrayMathGPU} from 'deeplearn';
import {Array1D, Array3D, Array4D, conv_util, ENV, NDArray} from 'deeplearn';

import {BenchmarkTest} from './benchmark';
import * as benchmark_util from './benchmark_util';
Expand All @@ -34,7 +34,7 @@ export interface DepthwiseConvParams extends ConvParams { channelMul: number; }

export class ConvGPUBenchmark implements BenchmarkTest {
async run(size: number, opType: string, params: ConvParams): Promise<number> {
const math = new NDArrayMathGPU();
const math = ENV.math;

const inDepth = params.inDepth;
const inShape: [number, number, number] = [size, size, inDepth];
Expand Down
12 changes: 8 additions & 4 deletions demos/benchmarks/matmul_benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* =============================================================================
*/
// tslint:disable-next-line:max-line-length
import {Array2D, NDArrayMathCPU, NDArrayMathGPU} from 'deeplearn';
import {Array2D, ENV, NDArrayMath} from 'deeplearn';

import {BenchmarkTest, LAST_RUN_CPU_CUTOFF_MS} from './benchmark';
import * as benchmark_util from './benchmark_util';

Expand All @@ -27,7 +28,9 @@ export class MatmulCPUBenchmark implements BenchmarkTest {
resolve(-1);
});
}
const math = new NDArrayMathCPU();
const safeMode = false;
const math = new NDArrayMath('cpu', safeMode);
ENV.setMath(math);
const a = Array2D.randUniform([size, size], -1, 1);
const b = Array2D.randUniform([size, size], -1, 1);
const start = performance.now();
Expand All @@ -41,8 +44,9 @@ export class MatmulCPUBenchmark implements BenchmarkTest {

export class MatmulGPUBenchmark implements BenchmarkTest {
async run(size: number): Promise<number> {
const math = new NDArrayMathGPU();

const safeMode = false;
const math = new NDArrayMath('webgl', safeMode);
ENV.setMath(math);
const a = Array2D.randNormal([size, size]);
const b = Array2D.randNormal([size, size]);

Expand Down
14 changes: 7 additions & 7 deletions demos/benchmarks/pool_benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

// tslint:disable-next-line:max-line-length
import {Array3D, conv_util, NDArrayMath, NDArrayMathCPU, NDArrayMathGPU} from 'deeplearn';
import {Array3D, conv_util, ENV, NDArrayMath} from 'deeplearn';
import {BenchmarkTest} from './benchmark';
import * as benchmark_util from './benchmark_util';

Expand Down Expand Up @@ -58,7 +58,9 @@ function getPoolingOp(option: string, math: NDArrayMath): (
export class PoolCPUBenchmark implements BenchmarkTest {
run(size: number, option: string,
params: PoolBenchmarkParams): Promise<number> {
const math = new NDArrayMathCPU();
const safeMode = false;
const math = new NDArrayMath('cpu', safeMode);
ENV.setMath(math);
const outputDepth = params.depth;
const xShape: [number, number, number] = [size, size, outputDepth];
const fieldSize = params.fieldSize;
Expand All @@ -83,8 +85,9 @@ export class PoolCPUBenchmark implements BenchmarkTest {
export class PoolGPUBenchmark implements BenchmarkTest {
async run(size: number, option: string, params: PoolBenchmarkParams):
Promise<number> {
const math = new NDArrayMathGPU();

const safeMode = false;
const math = new NDArrayMath('webgl', safeMode);
ENV.setMath(math);
const outputDepth = params.depth;
const xShape: [number, number, number] = [size, size, outputDepth];
const fieldSize = params.fieldSize;
Expand All @@ -93,11 +96,8 @@ export class PoolGPUBenchmark implements BenchmarkTest {
const op = getPoolingOp(option, math);

const benchmark = () => op(x, fieldSize, stride, 'same');

const time = await benchmark_util.warmupAndBenchmarkGPU(math, benchmark);

x.dispose();
math.dispose();

return time;
}
Expand Down
10 changes: 7 additions & 3 deletions demos/benchmarks/reduction_ops_benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* =============================================================================
*/
// tslint:disable-next-line:max-line-length
import {Array2D, NDArray, NDArrayMath, NDArrayMathCPU, NDArrayMathGPU, Scalar} from 'deeplearn';
import {Array2D, ENV, NDArray, NDArrayMath, Scalar} from 'deeplearn';

import {BenchmarkTest} from './benchmark';
import * as benchmark_util from './benchmark_util';
Expand All @@ -42,7 +42,9 @@ function getReductionOp(option: string, math: NDArrayMath): (input: NDArray) =>

export class ReductionOpsCPUBenchmark implements BenchmarkTest {
async run(size: number, option: string): Promise<number> {
const math = new NDArrayMathCPU();
const safeMode = false;
const math = new NDArrayMath('cpu', safeMode);
ENV.setMath(math);
const input = Array2D.randUniform([size, size], -1, 1);
const op = getReductionOp(option, math);
const start = performance.now();
Expand All @@ -58,7 +60,9 @@ export class ReductionOpsCPUBenchmark implements BenchmarkTest {

export class ReductionOpsGPUBenchmark implements BenchmarkTest {
async run(size: number, option: string) {
const math = new NDArrayMathGPU();
const safeMode = false;
const math = new NDArrayMath('webgl', safeMode);
ENV.setMath(math);
const input = Array2D.randUniform([size, size], -1, 1);
const op = getReductionOp(option, math);

Expand Down
11 changes: 8 additions & 3 deletions demos/benchmarks/unary_ops_benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/

// tslint:disable-next-line:max-line-length
import {Array2D, NDArray, NDArrayMath, NDArrayMathCPU, NDArrayMathGPU} from 'deeplearn';
import {Array2D, ENV, NDArray, NDArrayMath} from 'deeplearn';

import {BenchmarkTest} from './benchmark';
import * as benchmark_util from './benchmark_util';

Expand Down Expand Up @@ -61,7 +62,9 @@ function getUnaryOp(option: string, math: NDArrayMath) {

export class UnaryOpsCPUBenchmark implements BenchmarkTest {
async run(size: number, option: string): Promise<number> {
const math = new NDArrayMathCPU();
const safeMode = false;
const math = new NDArrayMath('cpu', safeMode);
ENV.setMath(math);
const input = Array2D.randUniform([size, size], -1, 1);
const op = getUnaryOp(option, math);
const start = performance.now();
Expand All @@ -77,7 +80,9 @@ export class UnaryOpsCPUBenchmark implements BenchmarkTest {

export class UnaryOpsGPUBenchmark implements BenchmarkTest {
async run(size: number, option: string) {
const math = new NDArrayMathGPU();
const safeMode = false;
const math = new NDArrayMath('webgl', safeMode);
ENV.setMath(math);
const input = Array2D.randUniform([size, size], -1, 1);
const op = getUnaryOp(option, math);

Expand Down
Loading