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

Update benchmarks to use EXT_disjoint_query_timer where available. #140

Merged
merged 5 commits into from
Sep 21, 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
2 changes: 1 addition & 1 deletion demos/benchmarks/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ export class BenchmarkRun {

export abstract class BenchmarkTest {
constructor(protected params?: {}) {}
abstract run(size: number, option?: string): number;
abstract run(size: number, option?: string): Promise<number>;
}
99 changes: 60 additions & 39 deletions demos/benchmarks/conv_benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import {initializeGPU} from '../../src/math/ndarray';
import {Conv2DProgram} from '../../src/math/webgl/conv_gpu';
import * as gpgpu_math from '../../src/math/webgl/gpgpu_math';
import {TextureManager} from '../../src/math/webgl/texture_manager';
import {Array1D, Array3D, Array4D, conv_util, GPGPUContext} from '../deeplearn';
// tslint:disable-next-line:max-line-length
import {Array1D, Array3D, Array4D, conv_util, ENV, GPGPUContext} from '../deeplearn';

import {BenchmarkTest} from './benchmark';

const OP_RUNS = 40;

export interface ConvBenchmarkParams {
inDepth: number;
outDepth: number;
Expand All @@ -39,45 +38,67 @@ export abstract class ConvBenchmark extends BenchmarkTest {
}

export class ConvGPUBenchmark extends ConvBenchmark {
run(size: number): number {
const gpgpu = new GPGPUContext();
const texManager = new TextureManager(gpgpu);
initializeGPU(gpgpu, texManager);
run(size: number): Promise<number> {
return new Promise<number>((resolve, reject) => {
const gpgpu = new GPGPUContext();
const texManager = new TextureManager(gpgpu);
initializeGPU(gpgpu, texManager);

const inDepth = this.params.inDepth;
const inShape: [number, number, number] = [size, size, inDepth];
const outDepth = this.params.outDepth;
const filterSize = this.params.filterSize;
const stride = this.params.stride;
const hasBias = true;
const convInfo = conv_util.computeConvInfo(
inShape, filterSize, filterSize, outDepth, stride, stride, 'same');
const program = new Conv2DProgram(convInfo, hasBias);
const outputShape = program.outputShape as [number, number, number];
const out = Array3D.zeros(outputShape);
const x = Array3D.randUniform(inShape, -1, 1);
const wShape =
conv_util.computeWeightsShape4D(1, outDepth, filterSize, filterSize);
const W = Array4D.randUniform(wShape, -1, 1);
const b = Array1D.randUniform([outDepth], -1, 1);
const inputs = [x, W, b];
const binary = gpgpu_math.compileProgram(gpgpu, program, inputs, out);

const benchmark = () => {
gpgpu_math.runProgram(binary, inputs, out);
};

const immediateCleanup = () => {
x.dispose();
W.dispose();
b.dispose();
out.dispose();
texManager.dispose();
gpgpu.deleteProgram(binary.webGLProgram);
};

const delayedCleanup = () => {
gpgpu.dispose();
};

if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER')) {
gpgpu.runBenchmark(benchmark).then((timeElapsed: number) => {
delayedCleanup();
resolve(timeElapsed);
});
immediateCleanup();
} else {
const start = performance.now();

const inDepth = this.params.inDepth;
const inShape: [number, number, number] = [size, size, inDepth];
const outDepth = this.params.outDepth;
const filterSize = this.params.filterSize;
const stride = this.params.stride;
const hasBias = true;
const convInfo = conv_util.computeConvInfo(
inShape, filterSize, filterSize, outDepth, stride, stride, 'same');
const program = new Conv2DProgram(convInfo, hasBias);
const outputShape = program.outputShape as [number, number, number];
const out = Array3D.zeros(outputShape);
const x = Array3D.randUniform(inShape, -1, 1);
const wShape =
conv_util.computeWeightsShape4D(1, outDepth, filterSize, filterSize);
const W = Array4D.randUniform(wShape, -1, 1);
const b = Array1D.randUniform([outDepth], -1, 1);
const inputs = [x, W, b];
const binary = gpgpu_math.compileProgram(gpgpu, program, inputs, out);
benchmark();
out.getValues();

const start = performance.now();
for (let i = 0; i < OP_RUNS; i++) {
gpgpu_math.runProgram(binary, inputs, out);
}
out.getValues();
const avgTime = (performance.now() - start) / OP_RUNS;
const totalTime = performance.now() - start;

x.dispose();
W.dispose();
b.dispose();
out.dispose();
texManager.dispose();
gpgpu.deleteProgram(binary.webGLProgram);
gpgpu.dispose();
immediateCleanup();
delayedCleanup();

return avgTime;
resolve(totalTime);
}
});
}
}
98 changes: 62 additions & 36 deletions demos/benchmarks/conv_transposed_benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import {initializeGPU} from '../../src/math/ndarray';
import {Conv2DDerInputProgram} from '../../src/math/webgl/conv_backprop_gpu';
import * as gpgpu_math from '../../src/math/webgl/gpgpu_math';
import {TextureManager} from '../../src/math/webgl/texture_manager';
import {Array3D, Array4D, conv_util, GPGPUContext} from '../deeplearn';
// tslint:disable-next-line:max-line-length
import {Array3D, Array4D, conv_util, ENV, GPGPUContext} from '../deeplearn';

import {BenchmarkTest} from './benchmark';

const OP_RUNS = 40;

export interface ConvTransposedBenchmarkParams {
inDepth: number;
outDepth: number;
Expand All @@ -39,41 +38,68 @@ export abstract class ConvTransposedBenchmark extends BenchmarkTest {
}

export class ConvTransposedGPUBenchmark extends ConvTransposedBenchmark {
run(size: number): number {
const origInputDepth = 1;
const origOutputDepth = 1;
const xShape: [number, number, number] = [size, size, origOutputDepth];
const fieldSize = 11;
const origStride = 1;
const origPad = 1;
run(size: number): Promise<number> {
return new Promise<number>((resolve, reject) => {
const origInputDepth = 1;
const origOutputDepth = 1;
const xShape: [number, number, number] = [size, size, origOutputDepth];
const fieldSize = 11;
const origStride = 1;
const origPad = 1;

const gpgpu = new GPGPUContext();
const texManager = new TextureManager(gpgpu);
initializeGPU(gpgpu, texManager);
gpgpu.enableAutomaticDebugValidation(true);

const convInfo = conv_util.computeConvInfo(
xShape, fieldSize, fieldSize, origOutputDepth, origStride, origStride,
origPad);
const program = new Conv2DDerInputProgram(convInfo);
const outputShape = program.outputShape as [number, number, number];
const out = Array3D.zeros(outputShape);
const x = Array3D.randUniform(xShape, -1, 1);
const wShape = conv_util.computeWeightsShape4D(
origInputDepth, origOutputDepth, fieldSize, fieldSize);
const W = Array4D.randUniform(wShape, -1, 1);
const inputs = [x, W];
const binary = gpgpu_math.compileProgram(gpgpu, program, inputs, out);

const benchmark = () => {
gpgpu_math.runProgram(binary, inputs, out);
};

const immediateCleanup = () => {
out.dispose();
x.dispose();
W.dispose();
texManager.dispose();
gpgpu.deleteProgram(binary.webGLProgram);
};

const delayedCleanup = () => {
gpgpu.dispose();
};

if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER')) {
gpgpu.runBenchmark(benchmark).then((timeElapsed: number) => {
delayedCleanup();
resolve(timeElapsed);
});
immediateCleanup();
} else {
const start = performance.now();

benchmark();
out.getValues();

const gpgpu = new GPGPUContext();
const texManager = new TextureManager(gpgpu);
initializeGPU(gpgpu, texManager);
gpgpu.enableAutomaticDebugValidation(true);
const totalTime = performance.now() - start;

const convInfo = conv_util.computeConvInfo(
xShape, fieldSize, fieldSize, origOutputDepth, origStride, origStride,
origPad);
const program = new Conv2DDerInputProgram(convInfo);
const outputShape = program.outputShape as [number, number, number];
const out = Array3D.zeros(outputShape);
const x = Array3D.randUniform(xShape, -1, 1);
const wShape = conv_util.computeWeightsShape4D(
origInputDepth, origOutputDepth, fieldSize, fieldSize);
const W = Array4D.randUniform(wShape, -1, 1);
const inputs = [x, W];
const binary = gpgpu_math.compileProgram(gpgpu, program, inputs, out);
const start = performance.now();
for (let i = 0; i < OP_RUNS; i++) {
gpgpu_math.runProgram(binary, inputs, out);
}
out.getValues();
const avgTime = (performance.now() - start) / OP_RUNS;
immediateCleanup();
delayedCleanup();

texManager.dispose();
gpgpu.deleteProgram(binary.webGLProgram);
gpgpu.dispose();
return avgTime;
resolve(totalTime);
}
});
}
}
81 changes: 52 additions & 29 deletions demos/benchmarks/logsumexp_benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,71 @@ import * as gpgpu_math from '../../src/math/webgl/gpgpu_math';
import {LogSumExpProgram} from '../../src/math/webgl/logsumexp_gpu';
import {TextureManager} from '../../src/math/webgl/texture_manager';
// tslint:disable-next-line:max-line-length
import {Array2D, GPGPUContext, NDArray, NDArrayMathCPU, Scalar} from '../deeplearn';
import {Array2D, ENV, GPGPUContext, NDArray, NDArrayMathCPU, Scalar} from '../deeplearn';

import {BenchmarkTest} from './benchmark';

const CPU_OPS_PER_RUN = 10;
const GPU_OPS_PER_RUN = 10;

export class LogSumExpCPUBenchmark extends BenchmarkTest {
run(size: number): number {
run(size: number): Promise<number> {
const math = new NDArrayMathCPU();
const a = NDArray.randUniform<Array2D>([size, size], -1, 1);
const start = performance.now();
for (let i = 0; i < CPU_OPS_PER_RUN; i++) {
math.logSumExp(a);
}
math.logSumExp(a);

const end = performance.now();
return (end - start) / CPU_OPS_PER_RUN;

return new Promise<number>((resolve, reject) => {
resolve(end - start);
});
}
}

export class LogSumExpGPUBenchmark extends BenchmarkTest {
run(size: number): number {
const gpgpu = new GPGPUContext();
const texManager = new TextureManager(gpgpu);
initializeGPU(gpgpu, texManager);
const out = new Scalar({texture: texManager.acquireTexture([1, 1])});
const a = Array2D.randUniform([size, size], -1, 1);
const program = new LogSumExpProgram(a.size);
const binary = gpgpu_math.compileProgram(gpgpu, program, [a], out);
run(size: number): Promise<number> {
return new Promise<number>((resolve, reject) => {
const gpgpu = new GPGPUContext();
const texManager = new TextureManager(gpgpu);
initializeGPU(gpgpu, texManager);
const out = new Scalar({texture: texManager.acquireTexture([1, 1])});
const a = Array2D.randUniform([size, size], -1, 1);
const program = new LogSumExpProgram(a.size);
const binary = gpgpu_math.compileProgram(gpgpu, program, [a], out);

const start = performance.now();
for (let i = 0; i < GPU_OPS_PER_RUN; i++) {
gpgpu_math.runProgram(binary, [a], out);
}
out.getValues();
const avgTime = (performance.now() - start) / GPU_OPS_PER_RUN;
a.dispose();
out.dispose();
texManager.dispose();
gpgpu.deleteProgram(binary.webGLProgram);
gpgpu.dispose();
const benchmark = () => {
gpgpu_math.runProgram(binary, [a], out);
};

const immediateCleanup = () => {
a.dispose();
out.dispose();
texManager.dispose();
gpgpu.deleteProgram(binary.webGLProgram);
gpgpu.deleteProgram(binary.webGLProgram);
};

const delayedCleanup = () => {
gpgpu.dispose();
};

if (ENV.get('WEBGL_DISJOINT_QUERY_TIMER')) {
gpgpu.runBenchmark(benchmark).then((timeElapsed: number) => {
delayedCleanup();
resolve(timeElapsed);
});
immediateCleanup();
} else {
const start = performance.now();

benchmark();
out.getValues();

const totalTime = performance.now() - start;

immediateCleanup();
delayedCleanup();

return avgTime;
resolve(totalTime);
}
});
}
}
Loading