Skip to content

Commit 0eba6de

Browse files
author
Nikhil Thorat
authored
Remove the global outer scope. (tensorflow#1425)
BUG
1 parent 546eafa commit 0eba6de

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/engine.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class Engine implements TensorManager, DataMover {
9797

9898
// Keep Tensors that parallel the tapes.
9999
private activeScope: ScopeState;
100-
private scopeStack: ScopeState[];
100+
private scopeStack: ScopeState[] = [];
101101
private keepTensors: Set<number> = new Set();
102102
private profiler: Profiler;
103103

@@ -112,9 +112,6 @@ export class Engine implements TensorManager, DataMover {
112112
constructor(
113113
public backend: KernelBackend, public safeMode: boolean,
114114
private debugMode: () => boolean) {
115-
// Create a default outer scope.
116-
this.activeScope = {track: [], name: 'default scope'};
117-
this.scopeStack = [this.activeScope];
118115
this.profiler = new Profiler(backend);
119116
this.activeProfile =
120117
{newBytes: 0, newTensors: 0, peakBytes: 0, kernels: [], result: null};
@@ -454,7 +451,7 @@ export class Engine implements TensorManager, DataMover {
454451

455452
const oldScope = this.scopeStack.pop();
456453
this.activeScope = this.scopeStack.length === 0 ?
457-
{track: [], name: 'default scope'} :
454+
null :
458455
this.scopeStack[this.scopeStack.length - 1];
459456

460457
// Track the current result in the parent scope.
@@ -612,7 +609,9 @@ export class Engine implements TensorManager, DataMover {
612609
'Safe mode is ON. Enclose all tensor operations inside tf.tidy(): ' +
613610
'tf.tidy(() => {op();...}); to avoid memory leaks.');
614611
}
615-
this.activeScope.track.push(result);
612+
if (this.activeScope != null) {
613+
this.activeScope.track.push(result);
614+
}
616615
return result;
617616
}
618617
}

src/engine_test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,3 +649,19 @@ describeWithFlags('Switching WebGL + CPU backends', WEBGL_ENVS, () => {
649649
expect(tf.memory().numTensors).toBe(0);
650650
});
651651
});
652+
653+
// NOTE: This describe is purposefully not a describeWithFlags so that we test
654+
// tensor allocation where no scopes have been created. The backend here must be
655+
// set to CPU because we cannot allocate GPU tensors outside a
656+
// describeWithFlags because the default webgl backend and the test backends
657+
// share a WebGLContext. When backends get registered, global WebGL state is
658+
// initialized, which causes the two backends to step on each other and get in a
659+
// bad state.
660+
describe('Memory allocation outside a test scope', () => {
661+
it('constructing a tensor works', () => {
662+
tf.setBackend('cpu');
663+
const a = tf.tensor1d([1, 2, 3]);
664+
expectArraysClose(a, [1, 2, 3]);
665+
a.dispose();
666+
});
667+
});

src/environment_test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ describeWithFlags(
6161
});
6262

6363
describeWithFlags('WEBGL_PAGING_ENABLED', WEBGL_ENVS, testEnv => {
64-
afterEach(() => {
65-
ENV.reset();
66-
ENV.setFeatures(testEnv.features);
67-
});
68-
6964
it('should be true if in a browser', () => {
7065
const features: Features = {'IS_BROWSER': true};
7166
const env = new Environment(features);

0 commit comments

Comments
 (0)