Skip to content

Commit 405befd

Browse files
authored
Speedup the WebGL tests by 2.5x (tensorflow#1625)
Speedup the WebGL tests by 2.5x. On my MacBook Pro 15inch, the WebGL 2.0 tests used to take 55 sec. Now they make 22sec. Same for WebGL 1.0. This PR's travis run is 5 mins 53 sec [link](https://travis-ci.org/tensorflow/tfjs-core/builds/506987446). Before it was 8mins and 33 secs [link](https://travis-ci.org/tensorflow/tfjs-core/builds/506838185) **Details** - Cache the WebGL binaries globally instead of re-compiling after every describe. - Fix a bug with the webgl debug mode being global, which caused most of the unit tests to run with gl.checkError() after every GL call. DEV PERF
1 parent 706d164 commit 405befd

File tree

7 files changed

+288
-228
lines changed

7 files changed

+288
-228
lines changed

src/kernels/backend_webgl.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ export interface WebGLTimingInfo extends TimingInfo {
137137
downloadWaitMs: number;
138138
}
139139

140+
const binaryCaches: {[webGLVersion: string]: {[key: string]: GPGPUBinary}} = {};
141+
142+
function getBinaryCache(webGLVersion: number) {
143+
if (webGLVersion in binaryCaches) {
144+
return binaryCaches[webGLVersion];
145+
}
146+
binaryCaches[webGLVersion] = {};
147+
return binaryCaches[webGLVersion];
148+
}
149+
140150
function mapActivationToShaderProgram(
141151
activation: Activation, packed = false): string {
142152
if (activation === 'linear') {
@@ -569,7 +579,7 @@ export class MathBackendWebGL implements KernelBackend {
569579
}
570580

571581
private textureManager: TextureManager;
572-
private binaryCache: {[key: string]: GPGPUBinary} = {};
582+
private binaryCache: {[key: string]: GPGPUBinary};
573583
private gpgpuCreatedLocally: boolean;
574584

575585
constructor(private gpgpu?: GPGPUContext) {
@@ -579,10 +589,12 @@ export class MathBackendWebGL implements KernelBackend {
579589

580590
if (gpgpu == null) {
581591
const gl = getWebGLContext(ENV.get('WEBGL_VERSION'));
592+
this.binaryCache = getBinaryCache(ENV.get('WEBGL_VERSION'));
582593
this.gpgpu = new GPGPUContext(gl);
583594
this.canvas = gl.canvas;
584595
this.gpgpuCreatedLocally = true;
585596
} else {
597+
this.binaryCache = {};
586598
this.gpgpuCreatedLocally = false;
587599
this.canvas = gpgpu.gl.canvas;
588600
}
@@ -2319,7 +2331,8 @@ export class MathBackendWebGL implements KernelBackend {
23192331
query = this.startTimer();
23202332
}
23212333

2322-
gpgpu_math.runProgram(binary, inputsData, outputData, customSetup);
2334+
gpgpu_math.runProgram(
2335+
this.gpgpu, binary, inputsData, outputData, customSetup);
23232336

23242337
const numBytesBeforePaging = ENV.get('WEBGL_NUM_MB_BEFORE_PAGING') * 1024;
23252338
if (pageToCpu && this.numBytesInGPU > numBytesBeforePaging) {
@@ -2363,15 +2376,13 @@ export class MathBackendWebGL implements KernelBackend {
23632376
if (this.disposed) {
23642377
return;
23652378
}
2366-
for (const key in this.binaryCache) {
2367-
this.gpgpu.deleteProgram(this.binaryCache[key].webGLProgram);
2368-
}
23692379
this.textureManager.dispose();
23702380
this.canvas.remove();
23712381
if (this.fromPixels2DContext != null) {
23722382
this.fromPixels2DContext.canvas.remove();
23732383
}
23742384
if (this.gpgpuCreatedLocally) {
2385+
this.gpgpu.program = null;
23752386
this.gpgpu.dispose();
23762387
}
23772388
this.disposed = true;

0 commit comments

Comments
 (0)