Skip to content

Commit

Permalink
Stop recording to the tape while a kernel runs (tensorflow#980)
Browse files Browse the repository at this point in the history
Some kernels might call higher-level ops to work around limitations. Because of this, we need to stop recording to the tape while the kernel runs.

Also use built-in inversesqrt in GLSL
  • Loading branch information
dsmilkov authored Apr 22, 2018
1 parent f519810 commit 8354c1c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,18 @@ export class Engine implements TensorManager {
};
const scopeName = this.activeScope.name;

// Stop recording to a tape when running a kernel.
this.customGradientDepth++;
if (!ENV.get('DEBUG')) {
result = forwardFunc(this.backend, saveFunc);
} else {
result = this.profiler.profileKernel(
scopeName, () => forwardFunc(this.backend, saveFunc));
}
// Continue recording after the kernel is done.
this.customGradientDepth--;

const recordKernel =
this.activeTape != null && this.customGradientDepth === 0;
if (recordKernel) {
if (this.shouldRecord()) {
const tapeNode: TapeNode = {
id: this.nextTapeNodeId++,
name: scopeName,
Expand Down
2 changes: 1 addition & 1 deletion src/kernels/webgl/batchnorm_gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class BatchNormProgram implements GPGPUProgram {
float variance = getVarianceAtOutCoords();
float offset = ${offsetSnippet};
float scale = ${scaleSnippet};
float inv = scale / sqrt(variance + float(${varianceEpsilon}));
float inv = scale * inversesqrt(variance + float(${varianceEpsilon}));
setOutput((x - mean) * inv + offset);
}
`;
Expand Down

0 comments on commit 8354c1c

Please sign in to comment.