Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tfjs-node] fixed summary writer memory leak #7490

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 8 additions & 5 deletions tfjs-node/src/nodejs_kernel_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,10 @@ export class NodeJSKernelBackend extends KernelBackend {
}
const opAttrs: TFEOpAttr[] =
[{name: 'T', type: this.binding.TF_ATTR_TYPE, value: typeAttr}];

this.binding.executeOp(
'WriteScalarSummary', opAttrs, this.getInputTensorIds(inputArgs), 0);
const ids = this.getInputTensorIds(inputArgs);
this.binding.executeOp('WriteScalarSummary', opAttrs, ids, 0);
// release the tensorflow tensor for Int64Scalar value of step
this.binding.deleteTensor(ids[1]);
});
}

Expand Down Expand Up @@ -594,8 +595,10 @@ export class NodeJSKernelBackend extends KernelBackend {
const typeAttr = this.typeAttributeFromTensor(buckets);
const opAttrs: TFEOpAttr[] =
[{name: 'T', type: this.binding.TF_ATTR_TYPE, value: typeAttr}];
this.binding.executeOp(
'WriteSummary', opAttrs, this.getInputTensorIds(inputArgs), 0);
const ids = this.getInputTensorIds(inputArgs);
this.binding.executeOp('WriteSummary', opAttrs, ids, 0);
// release the tensorflow tensor for Int64Scalar value of step
this.binding.deleteTensor(ids[1]);
});
}

Expand Down
19 changes: 19 additions & 0 deletions tfjs-node/src/tensorboard_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ describe('tensorboard', () => {
expect(fileNames.length).toEqual(1);
});

it('Writing tf.Scalar no memory leak', () => {
const writer = tfn.node.summaryFileWriter(tmpLogDir);
const beforeNumTFTensors = writer.backend.getNumOfTFTensors();
const value = scalar(42);
writer.scalar('foo', value, 0);
writer.flush();
value.dispose();
expect(writer.backend.getNumOfTFTensors()).toBe(beforeNumTFTensors);
});

it('No crosstalk between two summary writers', () => {
const logDir1 = path.join(tmpLogDir, '1');
const writer1 = tfn.node.summaryFileWriter(logDir1);
Expand Down Expand Up @@ -180,6 +190,15 @@ describe('tensorboard', () => {
expect(fileSize2 - fileSize1).toEqual(2 * incrementPerScalar);
});

it('summaryFileWriter no memory leak', () => {
const writer = tfn.node.summaryFileWriter(tmpLogDir);
const beforeNumTFTensors = writer.backend.getNumOfTFTensors();
const value = tensor1d([1, 2, 3, 4, 5], 'int32');
writer.histogram('foo', value, 0, 5);
writer.flush();
value.dispose();
expect(writer.backend.getNumOfTFTensors()).toBe(beforeNumTFTensors);
});
it('Can create multiple normal distribution', () => {
const writer = tfn.node.summaryFileWriter(tmpLogDir);
tf.tidy(() => {
Expand Down