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

Commit 1c20d78

Browse files
authored
Fix bug where gpgpu binary was holding onto stale inputs/output (#30)
* fix bug where gpgpu binary was holding onto stale inputs/output * address comments
1 parent f213377 commit 1c20d78

12 files changed

+64
-70
lines changed

demos/benchmarks/logsumexp_gpu_benchmark.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const BENCHMARK_TEST: BenchmarkTest = (size: number) => {
3333

3434
const start = performance.now();
3535
for (let i = 0; i < OP_RUNS; i++) {
36-
gpgpu_math.runProgram(binary);
36+
gpgpu_math.runProgram(binary, [a], out);
3737
}
3838

3939
const avgTime = (performance.now() - start) / OP_RUNS;

demos/benchmarks/mulmat_gpu_benchmark.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const BENCHMARK_TEST: BenchmarkTest = (size: number) => {
4747

4848
const start = performance.now();
4949
for (let i = 0; i < OP_RUNS; i++) {
50-
gpgpu_math.runProgram(binary);
50+
gpgpu_math.runProgram(binary, [aArr, bArr], resArr);
5151
}
5252
gpgpu.downloadMatrixFromTexture(resultTexture, size, size);
5353
const avgTime = (performance.now() - start) / OP_RUNS;

src/math/math_gpu.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class NDArrayMathGPU extends NDArrayMath {
8787
private gpgpu: GPGPUContext;
8888
private textureManager: TextureManager;
8989
private programCache: {[key: string]: WebGLProgram} = {};
90-
private binaryCache: {[key: string]: GPGPUBinary<NDArray, NDArray>} = {};
90+
private binaryCache: {[key: string]: GPGPUBinary} = {};
9191
private gpgpuCreatedLocally: boolean;
9292

9393
constructor(gpgpu?: GPGPUContext, safeMode = true) {
@@ -984,14 +984,12 @@ export class NDArrayMathGPU extends NDArrayMath {
984984
newShapeRCD, {texture: resultTexture, textureShapeRC: resultTexShape});
985985
}
986986

987-
private getAndSaveBinary<K extends NDArray, T extends NDArray>(
988-
key: string,
989-
getBinary: () => GPGPUBinary<K,T>):
990-
GPGPUBinary<K,T> {
987+
private getAndSaveBinary(key: string, getBinary: () => GPGPUBinary):
988+
GPGPUBinary {
991989
if (!(key in this.binaryCache)) {
992990
this.binaryCache[key] = getBinary();
993991
}
994-
return this.binaryCache[key] as GPGPUBinary<K, T>;
992+
return this.binaryCache[key];
995993
}
996994

997995
private getAndSaveProgram(programKey: string, getShaderSource: () => string):

src/math/webgl/argmaxequals_gpu_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function uploadArgMaxEqualsDownload(
2929
const out = Scalar.new(0);
3030
const program = new ArgMaxEqualsProgram(aArr.size, bArr.size);
3131
const binary = gpgpu_math.compileProgram(gpgpu, program, [aArr, bArr], out);
32-
gpgpu_math.runProgram(binary);
32+
gpgpu_math.runProgram(binary, [aArr, bArr], out);
3333
const result = out.get();
3434
aArr.dispose();
3535
bArr.dispose();

src/math/webgl/argminmax_gpu_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function uploadArgMinMaxDownload(
2929
const out = Scalar.new(0);
3030
const program = new ArgMinMaxProgram(arr.size, op);
3131
const binary = gpgpu_math.compileProgram(gpgpu, program, [arr], out);
32-
gpgpu_math.runProgram(binary);
32+
gpgpu_math.runProgram(binary, [arr], out);
3333

3434
const result = out.get();
3535
arr.dispose();

src/math/webgl/gpgpu_math.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {NDArray} from '../ndarray';
1717

1818
import {GPGPUContext} from './gpgpu_context';
1919
import * as shader_compiler from './shader_compiler';
20+
import {ShapeInfo} from './shader_compiler';
2021
import * as util from '../../util';
2122

2223
export interface GPGPUProgram {
@@ -26,47 +27,47 @@ export interface GPGPUProgram {
2627
userCode: string;
2728
}
2829

29-
export interface GPGPUBinary<T extends NDArray, K extends NDArray> {
30+
export interface GPGPUBinary {
3031
webGLProgram: WebGLProgram;
3132
program: GPGPUProgram;
3233
gpgpu: GPGPUContext;
3334
source: string;
34-
inputs: T[];
35-
output: K;
35+
inShapeInfos: ShapeInfo[];
36+
outShapeInfo: ShapeInfo;
3637
}
3738

3839
export function compileProgram<T extends NDArray, K extends NDArray>(
3940
gpgpu: GPGPUContext, program: GPGPUProgram, inputs: T[],
40-
output: K): GPGPUBinary<T,K> {
41+
output: K): GPGPUBinary {
4142
const userCode = program.userCode;
42-
const programInputs = program.variableNames.map((x, i) => {
43-
const fullShape = {
44-
shape: inputs[i].shape,
43+
const inputInfos = program.variableNames.map((x, i) => {
44+
const shapeInfo = {
45+
logicalShape: inputs[i].shape,
4546
texShape: inputs[i].getTextureShapeRC()
4647
};
47-
return {name: x, fullShape};
48+
return {name: x, shapeInfo};
4849
});
49-
50-
const outFullShape = {
51-
shape: output.shape,
50+
const inShapeInfos = inputInfos.map(x => x.shapeInfo);
51+
const outShapeInfo = {
52+
logicalShape: output.shape,
5253
texShape: output.getTextureShapeRC()
5354
};
54-
const source = shader_compiler.makeShader(programInputs, outFullShape,
55+
const source = shader_compiler.makeShader(inputInfos, outShapeInfo,
5556
userCode);
5657
return {
5758
program,
5859
source,
5960
webGLProgram: gpgpu.createProgram(source),
6061
gpgpu,
61-
inputs,
62-
output
62+
inShapeInfos,
63+
outShapeInfo
6364
};
6465
}
6566

66-
function validateBinaryAndProgram(aArrays: NDArray[], bArrays: NDArray[]) {
67-
aArrays.forEach((a, i) => {
68-
const shapeA = a.shape;
69-
const texShapeA = a.getTextureShapeRC();
67+
function validateBinaryAndProgram(shapeInfos: ShapeInfo[], bArrays: NDArray[]) {
68+
shapeInfos.forEach((s, i) => {
69+
const shapeA = s.logicalShape;
70+
const texShapeA = s.texShape;
7071
const shapeB = bArrays[i].shape;
7172
const texShapeB = bArrays[i].getTextureShapeRC();
7273

@@ -82,17 +83,10 @@ function validateBinaryAndProgram(aArrays: NDArray[], bArrays: NDArray[]) {
8283
}
8384

8485
export function runProgram<T extends NDArray, K extends NDArray>(
85-
binary: GPGPUBinary<T,K>, inputs?: T[], output?: K): void {
86-
if (inputs == null) {
87-
inputs = binary.inputs;
88-
} else {
89-
validateBinaryAndProgram(binary.inputs, inputs);
90-
}
91-
if (output == null) {
92-
output = binary.output;
93-
} else {
94-
validateBinaryAndProgram([binary.output], [output]);
95-
}
86+
binary: GPGPUBinary, inputs: T[], output: K): void {
87+
validateBinaryAndProgram(binary.inShapeInfos, inputs);
88+
validateBinaryAndProgram([binary.outShapeInfo], [output]);
89+
9690
const outTex = output.getTexture();
9791
const outTexShape = output.getTextureShapeRC();
9892
const gpgpu = binary.gpgpu;

src/math/webgl/logsumexp_gpu_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function uploadLogSumExpDownload(a: Float32Array, rows: number,
6666
const rScalar = Scalar.new(0);
6767
const program = new LogSumExpProgram(aArr.size);
6868
const binary = gpgpu_math.compileProgram(gpgpu, program, [aArr], rScalar);
69-
gpgpu_math.runProgram(binary);
69+
gpgpu_math.runProgram(binary, [aArr], rScalar);
7070
const result = rScalar.get();
7171
textureManager.dispose();
7272
gpgpu.deleteProgram(binary.webGLProgram);

src/math/webgl/minmax_gpu_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function uploadMinMaxDownload(a: Float32Array, rows: number,
2828
const out = Scalar.new(0);
2929
const program = new MinMaxProgram(arr.size, op);
3030
const binary = gpgpu_math.compileProgram(gpgpu, program, [arr], out);
31-
gpgpu_math.runProgram(binary);
31+
gpgpu_math.runProgram(binary, [arr], out);
3232
const result = out.get();
3333
arr.dispose();
3434
textureManager.dispose();

src/math/webgl/mulmat_gpu_test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ describe('mulmat_gpu (multiple matrices)', () => {
279279
gpgpu.uploadMatrixToTexture(b, bShape[0], bShape[1], bData);
280280
gpgpu.uploadMatrixToTexture(c, cShape[0], cShape[1], cData);
281281

282-
gpgpu_math.runProgram(axbProgram);
283-
gpgpu_math.runProgram(abxcProgram);
282+
gpgpu_math.runProgram(axbProgram, [aArr, bArr], abArr);
283+
gpgpu_math.runProgram(abxcProgram, [abArr, cArr], rArr);
284284
const result = gpgpu.downloadMatrixFromTexture(r, rShape[0], rShape[1]);
285285
const expected = test_util.cpuMultiplyMatrix(
286286
test_util.cpuMultiplyMatrix(aData, 4, 2, bData, 2, 12), 4, 12, cData,
@@ -362,7 +362,7 @@ export function uploadMultiplyMatrixDownload(
362362
gpgpu.uploadMatrixToTexture(aTexture, aNumRows, aNumCols, a);
363363
gpgpu.uploadMatrixToTexture(bTexture, bNumRows, bNumCols, b);
364364

365-
gpgpu_math.runProgram(binary);
365+
gpgpu_math.runProgram(binary, [aArr, bArr], resArr);
366366

367367
const result =
368368
gpgpu.downloadMatrixFromTexture(resultTexture, outNumRows, outNumCols);

src/math/webgl/reducesum_gpu_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function uploadReduceSumDownload(a: Float32Array, rows: number,
7979

8080
const program = new ReduceSumProgram(arr.size);
8181
const binary = gpgpu_math.compileProgram(gpgpu, program, [arr], out);
82-
gpgpu_math.runProgram(binary);
82+
gpgpu_math.runProgram(binary, [arr], out);
8383

8484
const result = out.get();
8585
arr.dispose();

0 commit comments

Comments
 (0)